Board index » delphi » GLOBAL HOT KEY - HELP NEEDED

GLOBAL HOT KEY - HELP NEEDED

I need a component or code outline to implement system-wide hot keys to enable
me to pop-up a small application no matter where I am in Windows 95/98.

I would load the program and minimize it at start-up and want to pop it up
when, for example, I press the F10 key.

Any help would be appreciated

Peter Yellow
London UK.

 

Re:GLOBAL HOT KEY - HELP NEEDED


Look at RegisterHotKey or thereabouts in the win32.hlp file... it should do
what you want.

HTH
  Roel

Quote
Stangate <stang...@aol.com> wrote:
> I need a component or code outline to implement system-wide hot keys to enable
> me to pop-up a small application no matter where I am in Windows 95/98.
> I would load the program and minimize it at start-up and want to pop it up
> when, for example, I press the F10 key.
> Any help would be appreciated
> Peter Yellow
> London UK.

Re:GLOBAL HOT KEY - HELP NEEDED


Hello All,

Yes use the function RegisterHotKey which can be found in the Win32
help file. Or, go to the Delphi Super Page and look for a free
component SysHotKeys (or close) which makes the job a little easier.

I have a question though. When using Hot Keys added through
RegisterHotKeys, I find that single hot keys, F1, F2..., ESC, etc.
work as expected, but hot keys such as ALT+A, CTL+ALT+F1, etc. have a
problem where the CTL or ALT modifies the behavior of subsequent
keyboard keys after the hot key stuff is executed. I am using the hot
key to begin a JournalPlayBack which enters character keystrokes into
Notepad.

I can use a function key, like F8, all day to repeatedly enter my
text, but if I make the hotkey something like CTL+ALT+B it works the
first time the hot key is pressed (enters the text into Notepad), but
does not work the second or future times the hotkey is pressed.

Using De{*word*81} breakpoints, MSVC's Spy, and writing the
JournalPlayBack records into a file, I can see that the
JournalPlayBack function is being started and that the proper
keystrokes are being sent to Notepad each time the hotkey is pressed.

I have noticed that if I make the hotkey SHFT+B and I set my playback
text to "aSDf" that the text will show up in Notepad as "aSDf" the
first time the hotkey is pressed, but as "AsdF" for subsequent hotkey
presses.

It is like the keyboard keyups and keydowns get out of sync with the
application. I see the same behaviour using Notepad or Netscape as my
target application.

I am using Delphi 3 pro. Any clues or help greatly appreciated.

Barry.

Quote
Roel Spapens <ro...@stack.nl> wrote:
>Look at RegisterHotKey or thereabouts in the win32.hlp file... it should do
>what you want.

>HTH
>  Roel

>Stangate <stang...@aol.com> wrote:
>> I need a component or code outline to implement system-wide hot keys to enable
>> me to pop-up a small application no matter where I am in Windows 95/98.

>> I would load the program and minimize it at start-up and want to pop it up
>> when, for example, I press the F10 key.

>> Any help would be appreciated

>> Peter Yellow
>> London UK.

Re:GLOBAL HOT KEY - HELP NEEDED


Have tried Syshotkey from super page but cannot get the component on the
pallette with Delphi 3.  Any ideas how to do it - I've E-mailed the author

Re:GLOBAL HOT KEY - HELP NEEDED


Hi Stangate...
Try to use API hooks!
/Joakim Andersson
Sweden

How to use Hooks
Ever write a program, and wish you could get the Windows message before
your form. Or maybe you wanted to respond to a keypress no matter where
you were in your program. If so, then maybe hooks are for you.

A Hook is defined in the Win API help as, "a point in the Microsoft?
Windows? message-handling mechanism where an application can install a
subroutine to monitor the message traffic in the system and process
certain types of messages before they reach the target window
procedure."

Basically the idea is, you want the message long before your form gets
it. Hooks are a means to intercept, or monitor messages. What you do
with the information is up to you. There are 12 hooks that you can
install, but we're going to focus on a keyboard hook. Mouse hooks could
also be useful in game programming, and are very similar in setup.
Our little program is going to call a function eveytime you press
<Shift-F1>. This function will be called no matter where in the program
you are. Our hook just displays a message box. Here's the keyboard hook
function :

     function KeyBoardCallback (Code : Integer; WParam : Word;

     LParam : Longint) : LongInt;

     begin

     { Setting the result to 0, tells windows to pass on the message }

     Result := 0;

     { Check to see if it's the hot key }

     If ((GetKeyState(HK_Key) < 0) and (GetKeyState(HK_Shift) < 0)) then

     begin

     { Here's where we do our stuff when the hotkey is pressed }

     ShowMessage('Hot key pressed');

     { So we don't get help by accident - because I'm using

     Shift-F1 as my hotkey }

     Result := 1;

     end;

     { call the next hook incase there are others }

     CallNextHookEx(gHook, code, Wparam, Lparam);

     end;

I've declared HK_Key and HK_Shift in the const section of the unit. This
makes it easier to change if later I decide I want a different key
combination. GetKeyState just checks to see if that's the key that is
being pressed. The
function should return 0 if we want our application to process the
message. If we don't return 0, our application would never see the
keypresses. CallNextHookEx calls the next hook in the chain. Even if
this is the only hook you've installed, you'll want to call this. Other
programs, or sections of your code might be calling hooks.
Now all we have to do is create the hook. This code is placed in your
project source file (*.dpr) before the application is created.

     { This sets up the hook }
     gHook := SetWindowsHookEx(WH_KEYBOARD,
@ClueCardKeyBoardCallback,      HInstance,   GetCurrentThreadId);

     If (gHook = 0) then ShowMessage('Not Hooked');

Then at the end of your program, you'll want to unhook the callback.
Here's the code to do this.

     { This unhooks the hook }

     If not(UnHookWindowsHookEx(gHook)) then

     ShowMessage('Couldnt Unhook');

That's all there is to hooks. I've made the source available so you can
see it in action. The program doesn't do much, but it does provide an
example to do even more powerful things. Happy hooking :)
Copyright ?1997 Daniel Spiess All Rights Reserved - Last updated 07
December 1997

Re:GLOBAL HOT KEY - HELP NEEDED


Thanks for your help
Happy Christmas

Other Threads