Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 08-25-2017, 05:03 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 33
Thumbs up [SOLVED] Listen to when a specific key is pressed in Windows then do something

Hello,

Can anyone link a tutorial on how I can have my software listen to a specific key (regardless where in windows it is pressed) and once that key is pressed it has to increase a TLabel caption to +1

For example:

I have on my main form a TLabel. I minimize the application and leave it running in my taskbar. I then proceed to do whatever it is I'm doing on my pc, the application should meanwhile 'listen' to if I ever press the 'L' key on my keyboard.

Once it detects I have pressed the L key, it should set the TLabel caption to 1.
If I press it again, it should set it to 2, thus incrementing with +1
If I press it again, it should set it to 3..and so on.

Basically a global key hook if I understand it correctly but I can't seem to find an example to increase the count, all I'm finding so far is to send another key press and intervene with what I'm doing and so forth.

Any help would be appreciated

Last edited by tcsh; 08-28-2017 at 03:43 PM.
  #2  
Old 08-25-2017, 07:57 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 173
Default

What you are looking for is a global keyboard hook and you would have to implement that in a DLL and call the DLL from your application. Just google

global keyboard hook example

and there are many examples on how to accomplish this.
  #3  
Old 08-26-2017, 01:34 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 33
Default

Quote:
Originally Posted by rojam View Post
What you are looking for is a global keyboard hook and you would have to implement that in a DLL and call the DLL from your application. Just google

global keyboard hook example

and there are many examples on how to accomplish this.
I mentioned I did. There aren't, or I couldn't find any, that deal specifically with a specific key - they're all for key combinations

Feel free to link one if you're sure there is one specific
  #4  
Old 08-27-2017, 03:06 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,058
Default

See
http://www.swissdelphicenter.ch/en/showcode.php?id=1722
__________________
Regards,
Abdulaziz Jasser
  #5  
Old 08-27-2017, 09:26 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 33
Default

Quote:
Originally Posted by Jasser View Post
Hello, thanks for the suggestion.

Quote:
I've tried it and it's not working:

Error on madExcept - I've taken care of that by installing it from the net (since I was missing the components)

Now once I run and generate the DLL it automatically erases madExcept from the uses clause in the code

and gives the following error: [dcc32 Error] HookLib.dpr(78): E2010 Incompatible types: 'NativeUInt' and 'Integer'

on the following line: hKeyHook := SetWindowsHookEx(WH_KEYBOARD, KeyHookFunc, hInstance, 0);

No clue how to get this to work / modify the code to trap only the L and not all the keys as it's currently doing

Using Rad Studio Tokyo 10.2

Later edit: Managed to get it working, was missing an @ at KeyHookFunc. Further, by tracing the tutorial you linked I stumbled across this: http://www.delphipraxis.net/1031080-post3.html

Solved it. Solution was to add: If VirtualKey = VK_F4 then (and replace VK_F4) with the key I want (my case Ord('L') right after Count: = ToAscii (VirtualKey, KeyStroke, KeyState1, AryChar, 0);

In case anyone finds this topic later on.

Again, thanks for the help Jasser, always a pleasure!

Last edited by tcsh; 08-28-2017 at 03:43 PM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:18 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.