Solved

About key trapping.....

Posted on 1998-05-29
7
1,980 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 50 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 304
code issue 8 130
Mobile Keyboard covers the display of the TMemo 3 87
how can i search if string exist in array ? 3 58
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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