?
Solved

About key trapping.....

Posted on 1998-05-29
7
Medium Priority
?
1,986 Views
Last Modified: 2008-02-01
Any experts know how to trap:
a. TAB in TEdit
   I've used On Keypressed, On Keydown and On Keyup, but    those are failed, although I've used VK_TAB and #9    constant.
b. ALT-F4
c. If in Turbo Pascal, we can capture extended key by this    way
     ch:= readkey;
     if ch = #0 then begin
       ch:= readkey;
       case ch of
        ....
       end;
    end;
   
   How can we catch the extended key in Delphi?

Thanks experts 4 your kindness,

Daniel    
   
0
Comment
Question by:daniel_c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:rickpet
ID: 1349199
Daniel

The only keystroke that you can see is on wm_keyup, but by that time it's to late.  This makes it impossible to tie into KeyUp, KeyDown, KeyPress, or WndProc.

There is a couple of ways to handle this...One use TPopupMenu and let it capture the keystrokes for you...

If that's not to your liking you can tie into TApplication.OnMessage.

Declared in the Form's interface
procedure MyMessage(var Msg: TMsg; var Handled: Boolean);

On Form Create add...
Application.OnMessage := MyMessage;

procedure TForm1.MyMessage(var Msg: TMsg; var Handled: Boolean);
begin
if (Screen.ActiveControl is TEdit) and (Msg.message = WM_KeyDown) then
  begin
    if (Msg.WParam = vk_tab)then
      Handled := true;
  end;
end;

You can use this to handle any virtual key, but be careful as this see's every message that goes through your program.

Rick
0
 
LVL 7

Author Comment

by:daniel_c
ID: 1349200
I need clearer explanation. Besides, my others 2 questions aren't answered yet.

What I want is if I press TAB key in TEdit, I will change it into other key.
  IF key = VK_TAB THEN
    key:= ...

That's all. But it didn't work. Rickpet, I need clearer explanation or if you are willing, give me a simple source code.
BTW, thanks Rickpet 4 your kindness. But I still need clearer answer.

Daniel
0
 
LVL 3

Expert Comment

by:daitt
ID: 1349201
Dear Daniel,
You should built a system keyboard hook and place it in a DLL library, so that you can trap every keyboard event occurs in the system. About keyboard hook, you can read in Borland Help files (include in BP70 or BC40).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:rickpet
ID: 1349202
Daniel...

Note most keystrokes you can just watch in Keydown or Keypress
in KeyDown you watch virtual keys...in Keypress you watch the actual char value.

Now this works for most keystrokes...there are exceptions to this. Tab like characters(i.e. tab, arrow keys) will be passed ahead to the next component.  Also the Alt key is handled differently because it is passed to the menus or hotkeys.

The easiest and simplest way to handle extended keystrokes is to use TPopUpMenu.  The keystrokes are routed through this component before they finally trickle down to your component, if they are even passed down.

There is a neat function called ShortCut.  This will convert a virtual keystroke into a TShortCut which is the parameter for a ShortCut for a TMenuItem.


Rick
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1349203
Daniel...

Note most keystrokes you can just watch in Keydown or Keypress
in KeyDown you watch virtual keys...in Keypress you watch the actual char value.

Now this works for most keystrokes...there are exceptions to this. Tab like characters(i.e. tab, arrow keys) will be passed ahead to the next component.  Also the Alt key is handled differently because it is passed to the menus or hotkeys.

The easiest and simplest way to handle extended keystrokes is to use TPopUpMenu.  The keystrokes are routed through this component before they finally trickle down to your component, if they are even passed down.

There is a neat function called ShortCut.  This will convert a virtual keystroke into a TShortCut which is the parameter for a ShortCut for a TMenuItem.


Rick
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1349204
Daniel

(more on TPopUpMenu-MenuItems also have a visible property so that your users don't have to see the hotkey)

Now if you don't want to use a TPopUpMenu you can handle all messages at Application.OnMessage. This handles Tab keys...and can be used to hook into arrow keys, etc...

On Form Create add...
Application.OnMessage := MyMessage;

procedure TForm1.MyMessage(var Msg: TMsg; var Handled: Boolean);
begin
if (Screen.ActiveControl is TEdit) and (Msg.message = WM_KeyDown) then
  begin
    if (Msg.WParam = vk_tab)then
    Handled := true;
  end;
end;


Now to handle your Alt-F4

you can do this at the actual control...onKeyPress

  if (Shift = [ssAlt]) and (Key = vk_F4) then
    ShowMessage('Alt-F4');


Anymore Questions???

Rick
0
 

Accepted Solution

by:
MAVEN earned 200 total points
ID: 1349205
Private
procedure kx( var Message: TCMDialogkey ); message CM_DIALOGKEY;


procedure TForm1.kx( var Message: TCMDialogKey );
begin
  with Message do
    case CharCode of
      VK_TAB: begin
                {Do whatever you want!}    
                exit
              end  
                     
    end;
  //if you call inherited here you do the default processing
  //of the key. If you bypass inherited, you handle the key
  //all by yourself.
  inherited;
end;

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question