Windows 7 x64 上的 GetAsyncKeyState
我正在尝试在 windows7 x64 上使用 C# 的 GetAsyncKeyState(i) 来获取按下的按键。它在 x86 上完美运行。这是我的代码:
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(long vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
search = false;
int key_my;
for (i = 0; i < 255; i++)
{
key_my = GetAsyncKeyState(i); // this should return -3.... but it does 46...........
if ( key_my == (System.Int16.MinValue + 1))
{ search = true; break; }
}
if ( search == true)
{
...//using if to keys here.
}
有什么想法吗?
I'm trying to use GetAsyncKeyState(i) on windows7 x64 with C# to get pressed keys. It works perfect on x86. Here are my codes:
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(long vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
search = false;
int key_my;
for (i = 0; i < 255; i++)
{
key_my = GetAsyncKeyState(i); // this should return -3.... but it does 46...........
if ( key_my == (System.Int16.MinValue + 1))
{ search = true; break; }
}
if ( search == true)
{
...//using if to keys here.
}
any IDEA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GetAsyncKeyState
函数的返回类型应为short
而不是int
,并且其参数应输入为int
而不是 <代码>长The
GetAsyncKeyState
function should have a return type ofshort
notint
and it's parameter should be typed toint
notlong
尽管这个问题已经得到了正确的回答,但我想指出我使用这个答案的经验,并且将它放在一行中不起作用。
不不工作。
确实有效。
(我不知道为什么。)
Though this question has already been answered correctly, I'd like to point out my experience using this answer and that putting it in one line does not work.
does NOT work.
does work.
(I don't know why.)