Solved

About key trapping.....

Posted on 1998-05-29
7
1,979 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE2 application frozen on Windows 10 10 274
code issue 8 99
Delphi selector screen 2 69
Graphics32 under Delphi 10.1 Berlin 2 36
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

23 Experts available now in Live!

Get 1:1 Help Now