Solved

About key trapping.....

Posted on 1998-05-29
7
1,977 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now