Re: How to implement listener for F7 key

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance





Roger wrote:
> I have an application that I want to run in the background until F7 key is
> pressed. Does anyone know how to implement a listener for F7 keypress to
> activate application?

win32 api has a RegisterHotKey, to listen for CTRL+ALT+SHIFT+D:

internal class Win32
{
[DllImport("user32.dll", SetLastError=true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int
hotkeyId, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int hotkeyId);
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
}

public class SomeForm {
...
protected int hotkeyid = SOME_INTEGER_ID;
protected bool hotkey_registered;
SomeForm()
{
hotkey_registered = Win32.RegisterHotKey(Handle, hotkeyid,
Win32.KeyModifiers.Control | Win32.KeyModifiers.Alt |
Win32.KeyModifiers.Shift, Keys.D);
}
protected override void Dispose( bool disposing ) {
if ( hotkey_registered )
Win32.UnregisterHotKey(Handle, hotkeyid);
...
}

protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
hotkeyPressed(ref m);
break;
}
base.WndProc(ref m );
}
public void hotkeyPressed(ref Message m) { ... }
}

In the docs for RegisterHotKey the following is said of hotkeyid:

"[in] Specifies the identifier of the hot key. No other hot key in the
calling thread should have the same identifier. An application must
specify a value in the range 0x0000 through 0xBFFF. A shared
dynamic-link library (DLL) must specify a value in the range 0xC000
through 0xFFFF (the range returned by the GlobalAddAtom function). To
avoid conflicts with hot-key identifiers defined by other shared DLLs, a
DLL should use the GlobalAddAtom function to obtain the hot-key
identifier. "

--
Helge Jensen
mailto:helge.jensen@xxxxxxx
sip:helge.jensen@xxxxxxx
-=> Sebastian cover-music: http://ungdomshus.nu <=-
.



Relevant Pages

  • Re: GPRS Connection status [PPC 2003]
    ... >> is in use by another connection ... >>> public static extern bool EstablishSynchronousGPRSConnection(ref ... >> phConnection, ref uint pdwStatus, ref bool pinAvailable); ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: pinvoke SetCommState Windows CE 5.0
    ... public const uint GENERIC_WRITE = 0x40000000; ... int nNumberOfBytesToWrite, ... public static extern Boolean ReadFile(int hFile, ... public static extern bool CloseHandle; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: pinvoke SetCommState Windows CE 5.0
    ... public const uint GENERIC_WRITE = 0x40000000; ... int nNumberOfBytesToWrite, ... public static extern Boolean ReadFile(int hFile, ... public static extern bool CloseHandle; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Verify a Windows Account
    ... static bool authUserLocal(string username, string password) ... public static extern bool LogonUser(string lpszUsername, ... int dwLogonType, int dwLogonProvider, out int phToken); ...
    (microsoft.public.dotnet.general)
  • Verify a Windows Account
    ... static bool authUserLocal(string username, string password) ... public static extern bool LogonUser(string lpszUsername, ... int dwLogonType, int dwLogonProvider, out int phToken); ...
    (microsoft.public.dotnet.framework)