@的虚拟钥匙?密钥注入 C#
我正在尝试将密钥注入到程序中。我已成功注入按键,但找不到 @ 符号的虚拟按键代码。我发现这个网站非常有用,但仍然找不到它。 Keys.Send();不是一个选项,因为我不想选择窗口,我希望能够在后台完成这一切。
到目前为止,这是我的代码:
const UInt32 WM_KEYDOWN = 0x0100;
int VK_A = 0x41;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process proc in processes)
{
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_A, 0);
}
}
这会将 A 键发送到记事本。
任何帮助将不胜感激。
I am trying to inject keys into a program. I have managed to inject the keys but I cannot find the Virtual Key Code for the @ sign. I have found this site that was very useful but still can't find it. Keys.Send(); is not an option as I do not want the window selected, I want to be able to do it all in the background.
This is my code so far:
const UInt32 WM_KEYDOWN = 0x0100;
int VK_A = 0x41;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process proc in processes)
{
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_A, 0);
}
}
This would send the A key to notepad.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Windows 为用户可以按下的每个键定义了特殊常量,称为虚拟键代码。
您的键盘上没有@键,而是使用Shift+2(至少在我的键盘上)来执行此操作。
我认为虽然你不能使用 PostMessage 模拟 Shift+2。
请参阅 http://blogs.msdn.com/b/ oldnewthing/archive/2005/05/30/423202.aspx。
Windows defines special constants for each key the user can press, called virtual key codes.
You don't have a @ key on your keyboard, instead you use Shift+2 (at least on my keyboard) to do it.
I think though you can't simulate Shift+2 using PostMessage.
See http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx.
@
本身没有对应的键。您可以发送对应的键+修饰符键状态,在美式键盘上为 Shift+2。但在其他键盘布局上,按键是不同的。例如,在德国布局中,它是 AltGr+Q。
或者您可以直接发送 WM_CHAR。这适用于大多数应用程序,但有些(通常是游戏)不会接受它。
我会先尝试
WM_CHAR
,只有当不起作用时才切换到 WM_KEYDOWN。@
has no corresponding key by itself. You can send the key(s)+modifier key state corresponding to it, which is Shift+2 on American keyboards.But on other keyboard layouts the keys are different. For example on the German layout it's AltGr+Q.
Or you can send WM_CHAR directly. That will work with most applications, some(typically games) won't accept it though.
I'd try
WM_CHAR
first, and only if that doesn't work switch to WM_KEYDOWN.@
本身并不是一个键,而是一个“替代”字符,这意味着它只能在按下修饰键(shift)时应用;因此,您可能需要组合两个值:另一种可能性是您可能需要按一系列调用中的每个键。
无论哪种方式,您都需要多个密钥代码。
虽然我确信您必须采用我上面提到的第二种方法(组合这些值不太可能起作用),但我有点担心您实际上如何设法将字符发布到
notepad
首先,因为您使用的是MainWindowHandle
而不是编辑器句柄 - 以防万一您遇到任何尚未注意到的麻烦,以下代码会获取记事本文本区域和允许您向其“发布”:@
isn't a key in an of itself and is an 'alternate' character, meaning it can only be applied when a modifier key is pressed (shift); so, you might need to combine two values:Another possibility is that you might need to push each key in a sequence of calls.
Either way, you need more than a single key code.
Though I'm sure you'll have to take the second approach I mentioned above (combining the values isn't likely to work), I've become a little concerned as to how you've actually managed to post characters to
notepad
in the first place, seeing as you're using theMainWindowHandle
as opposed to the editor handle - just in case you run into any troubles not yet noticed, the following code grabs the notepad text area and does allow you to 'post' to it:这是一个替代解决方案 - 查找外部应用程序中使用的键盘,并将字符映射到适当的键(如果可用)。
C/C++ 我没有方便的 C# 定义
另外,如果您想在不被用户中断的情况下发送多个按键(或鼠标单击),则 SendInput 可能是更好的选择。
Here is an alternate solution - find what keyboard is in use in the foreign app and map the character to an appropriate key if available.
C/C++ I don't have a handy C# definitions
Also, if you want to send several keys (or mouse clicks) without interruption by the user then SendInput might be a better option.