问题
I am creating a program that allows you to paste text but it will look like you are typing it as it pastes, Paste Typer.
I am wanting to use Ctrl + b
to set off the pasting, but I am having issues with hotkeys.
I think what I need is to use a WH_KEYBOARD_LL
hook and include that user32 file but I usually get errors when I add it and the namespace.
I have come closest with this: http://thedarkjoker94.cer33.com/?p=111 - but it doesn't seem to work with Ctrl
, alt
and other modifiers, even when I used KeyData
.
I need a way to make a hotkey that will work even when the program is not the active window. This is a windows forms application in Microsoft Visual C# 2010.
There a rea lot of StackOverlow topics but they are dated and are not complete enough for me to get something running.
回答1:
Are you talking about the HookManager?
I use it successfully like this:
HookManager.KeyDown += new KeyEventHandler(HookManager_KeyDown);
and
void HookManager_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Keys.LWin)) // Is the Left-Windows Key down and ...
switch (e.KeyCode)
{
case Keys.O:
// ...
e.Handled = true;
break;
case Keys.H:
// ...
e.Handled = true;
break;
}
}
I created the Keyboard class here is the code:
// Used: http://www.pinvoke.net/default.aspx/user32.getasynckeystate
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Win32.Devices
{
public class Keyboard
{
[DllImport("user32.dll")]
static extern ushort GetAsyncKeyState(Keys vKey);
public static bool IsKeyDown(Keys key)
{
return 0 != (GetAsyncKeyState(key) & 0x8000);
}
}
}
来源:https://stackoverflow.com/questions/8765906/global-hooks-non-active-program