[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1993
  • Last Modified:

About key trapping.....

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
daniel_c
Asked:
daniel_c
1 Solution
 
rickpetCommented:
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
 
daniel_cAuthor Commented:
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
 
daittCommented:
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
Technology Partners: 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!

 
rickpetCommented:
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
 
rickpetCommented:
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
 
rickpetCommented:
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
 
MAVENCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now