Pergunta

I can't find on google what reference I must use to be able to use RegisterHotKey. What is it?

And while on the topic, should I use RegisterHotKey if I am trying to create an application which listen for key combination in the background?

Foi útil?

Solução

You need a DllImport, not just a reference. You can find tons more information at pinvoke.net.

In short, if you add:

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

somewhere in your program, the only remaining tricky part is coming up with the hWnd to register to handle the key. The sample code linked at pinvoke.net above should help you use the DllImport.

Outras dicas

Here is what you will need to use the RegisterHotKey function from C#:

/// <summary> The RegisterHotKey function defines a system-wide hot key </summary>
/// <param name="hwnd">Handle to the window that will receive WM_HOTKEY messages generated by the hot key.</param>
/// <param name="id">Specifies the identifier of the hot key.</param>
/// <param name="fsModifiers">Specifies keys that must be pressed in combination with the key specified by the 'vk' parameter in order to generate the WM_HOTKEY message.</param>
/// <param name="vk">Specifies the virtual-key code of the hot key</param>
/// <returns><c>true</c> if the function succeeds, otherwise <c>false</c></returns>
/// <seealso cref="http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx"/>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top