Global Hotkey? Similar to RegisterHotKey

  • Thread starter Thread starter John Shaw
  • Start date Start date
J

John Shaw

Is there a way to trap the keyboard input on a system
basis that would allow a hot key to be created that would
allow a specific form to respond even if it does not have
focus in the .Net framework? I guess this would be similar
to the RegisterHotKey Method --a system wide hotkey.
Example-- I need to control the windows media player, used
in a form, from the keyboard even when it does not have
focus.
 
Hello John,

I am glad to hear it.

Thanks very much for participating the community.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "John Shaw" <[email protected]>
!Sender: "John Shaw" <[email protected]>
!References: <[email protected]>
<[email protected]>
!Subject: RE: Global Hotkey? Similar to RegisterHotKey
!Date: Mon, 8 Sep 2003 12:17:34 -0700
!Lines: 66
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcN2PdvWgBgnAbF2SOaDKgelxSxnIg==
!Newsgroups: microsoft.public.dotnet.framework
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:53214
!NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Thanks for you help. Works fine!
!>-----Original Message-----
!>Hello John,
!>
!>Thanks for posting in the group.
!>
!>In order to hook system information, we could use Win32
!system hooks.
!>However, in order to hook system keyboard/mouse
!information, the hook
!>application should reside in a DLL. So it is not possilbe
!to use Win32
!>system hooks in .NET programming.
!>
!>However, for RegisterHotkey API, I think we could use it
!in .NET. In order
!>to call it, we need to use PInvoke in .NET programming.
!>
!>Hope that helps.
!>
!>Best regards,
!>Yanhong Huang
!>Microsoft Online Partner Support
!>
!>Get Secure! - www.microsoft.com/security
!>This posting is provided "AS IS" with no warranties, and
!confers no rights.
!>
!>--------------------
!>!Content-Class: urn:content-classes:message
!>!From: "John Shaw" <[email protected]>
!>!Sender: "John Shaw" <[email protected]>
!>!Subject: Global Hotkey? Similar to RegisterHotKey
!>!Date: Wed, 3 Sep 2003 10:36:31 -0700
!>!Lines: 8
!>!Message-ID: <[email protected]>
!>!MIME-Version: 1.0
!>!Content-Type: text/plain;
!>! charset="iso-8859-1"
!>!Content-Transfer-Encoding: 7bit
!>!X-Newsreader: Microsoft CDO for Windows 2000
!>!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!>!Thread-Index: AcNyQemR+hrtPlidRiyDRNzTNd/y5Q==
!>!Newsgroups: microsoft.public.dotnet.framework
!>!Path: cpmsftngxa06.phx.gbl
!>!Xref: cpmsftngxa06.phx.gbl
!microsoft.public.dotnet.framework:52878
!>!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!>!X-Tomcat-NG: microsoft.public.dotnet.framework
!>!
!>!Is there a way to trap the keyboard input on a system
!>!basis that would allow a hot key to be created that
!would
!>!allow a specific form to respond even if it does not
!have
!>!focus in the .Net framework? I guess this would be
!similar
!>!to the RegisterHotKey Method --a system wide hotkey.
!>!Example-- I need to control the windows media player,
!used
!>!in a form, from the keyboard even when it does not have
!>!focus.
!>!
!>
!>.
!>
!
 
This seems to work, alright for what you were originally doing...

hMod = WindowsAPI.GetModuleHandle("HookTest.exe");
hHook =
WindowsAPI.SetWindowsHookEx(WindowsAPI.SystemHooks.WH_KEYBOARD_LL,
new WindowsAPI.HOOKPROC(this.hookProc), hMod , 0);

The function this.hookProc is then called....

I don't know why I have to use WH_KEYBOARD_LL instead of just
WH_KEYBOARD, but it seems to work, so I won't complain. All the
WondowsAPI stuff is just a class that holds static calls to Windows API.
I don't know if there is a pre-built equivalent in .Net or not.

Obviously HookTest.exe is the name of my exe module, which is what you
need. If there is a way of getting the application file name at
run-time I would recommend doing that.

All my function does at this time is beep, so I don't know if there are
as yet unknown quirks to what I am doing.

Good Luck,
Heath Beimdiek
 
WH_KEYBOARD_LL will only work in NT 4.0, Win 2k, and Win XP...
Also if you want any really useful information and you use this hook,
you need to recieve the structure as a pointer.

If anyone knows why WH_KEYBOARD doesn't seem to work, I would appreciate
it. I get a handle, its just that it never seems to call the hook function.
 
You can use P/Invoke to use the Win32 API function RegisterHotkey. By inheriting NativeWindow you can then respond to any WM_HOTKEY message sent to your message queue.

Regards, Jakob.
 
Might have to try that, after some time away from the code, all of a
sudden the code I posted doesn't seem to work so well, I wonder if I
have to duplicate the handle...

I swear it was working pretty darn good...

I suppose my usual saying "if it didn't happen twice it didn't happen"
with reguards to windows errors applies here too:(

Later,
Heath Beimdiek
 
The reason why WH_KEYBOARD does not work:

The WH_KEYBOARD will work on a per thread basis just as any other
hook will work. When you want to collect the keystroke system-wide,
however, you need to put the functionality and callback procedure into a
dll so that windows may inject that dll(load into a process) when the
hook is notified that the event has occured.


The reason why WH_KEYBOARD_LL works:

This is a special exception to the way in which system hooks behave.
This can be extremely useful, but may also have adverse effects that you
may not be aware of at first. When calling this hook - it must be
called system-wide or it will fail. You may call this directly from VB
with no dll at all. The reason this happens is that you hijack the
message queue of the system basically. Instead of messages going to
different queues for each thread, they are filtered through your
callback routine first. This is very dangerous and neat. By goind
through just your routine and not into separate message queues of each
thread you are taking away the functionality of multitasking in the
Win32 paradigm so use wisely. This is the way win16 used to do it all
the time - one message queue for the entire system. Since they all go
through your callback routine - make sure you call CallNextHookEx after
you finish processing the information.

Peace
 
Back
Top