I'm writing, just to get confidence with the programming procedures, a win32 application that responds to keyboard input. To do so I'm using the GetAsyncKeyState()
function.
我正在写,只是为了对编程程序有信心,这是一个响应键盘输入的win32应用程序。为此,我正在使用GetAsyncKeyState()函数。
At first I wrote all my code in the main()
function, and all seemed well, it worked. So I decided to complicate things, but that requires me to use the GetAsyncKeyState()
function in a different function called by the main()
. I thought I just needed to declare some variables outside the main()
and move the code from the main
to the new function like this:
起初我在main()函数中写了我的所有代码,一切看起来都很好,它起作用了。所以我决定复杂化,但这需要我在main()调用的不同函数中使用GetAsyncKeyState()函数。我以为我只需要在main()之外声明一些变量,并将代码从main移动到新函数,如下所示:
int btnup_down = 0;
int close = 1;
int main(void){
while (1){
Sleep(50);
listentokb();
if (close == 0){
break;
}
}return 0;
}
int listentokb(void){
if ((GetAsyncKeyState(0x4C) & 0x8000) && (ko == 0)){
ko = 1;
printf("Ok you pressed k");
return 0;
} else if (((GetAsyncKeyState(0x4C) == 0) && (ko == 1)) {
ko = 0;
printf("Now you released it");
close = 0;
return 0;
}return 0;
}
When I run this code, the loop keeps going and it doesn't matter if I press the key or not, it keeps looping without printf-ing anything. Any help will be greatly appreaciated.
当我运行这段代码时,循环继续运行,如果我按下键是无关紧要的,它会保持循环而不打印任何东西。任何帮助都会受到很大的影响。
1 个解决方案
#1
Your problem has nothing to to with main()
or not. You can call winapi function such as GetAsyncKeyState()
from wherever you want in your code, as long as you provide the good arrguments.
你的问题与main()无关。您可以在代码中的任何位置调用Winapi函数,例如GetAsyncKeyState(),只要您提供良好的参数即可。
According to this list of virtual key codes the code 0x4c corresponds to the key L and not to the key K. So after a bracket correction typo in your code, I could run it succesfully interupting the loop with L
根据这个虚拟键码列表,代码0x4c对应于键L,而不是键K.因此,在代码中的括号修正拼写错误之后,我可以成功运行它,使用L替换循环
Some remarks about your function:
关于你的功能的一些评论:
Your function listentokb()
always returns 0. On the other hand, you use a global variable close
to tell the calling function the result of your keyboard scanning. This is a very bad practice: whenever possible avoid global variables.
你的函数listentokb()总是返回0.另一方面,你使用一个全局变量close来告诉调用函数键盘扫描的结果。这是一种非常糟糕的做法:尽可能避免全局变量。
Here a slightly updated version of your code that bans global variables, and use return value to communicate the results:
这里是一个略微更新的代码版本,它禁止全局变量,并使用返回值来传达结果:
const int KEY_K = 0x4B; // avoid using values directly in the code
int listentokb (void){ // returns 'K' if K is released and 0 otherwise
static int ko; // this is like a global variable: it will keep the value from one call to the other
// but it has teh advantage of being seen only by your function
if((GetAsyncKeyState(KEY_K) & 0x8000) && (ko == 0)){
ko = 1;
printf("Ok you pressed k");
return 0;
}
else if((GetAsyncKeyState(KEY_K) == 0) && (ko == 1)) {
ko = 0;
printf("Now you released it");
return 'K';
}
return 0;
}
int main(void){
bool go_on = true; // The state of the loop shall be local variable not global
while(go_on){
Sleep(50);
go_on= ! listentokb(); // if returns 0 we go on
}
return 0;
}
#1
Your problem has nothing to to with main()
or not. You can call winapi function such as GetAsyncKeyState()
from wherever you want in your code, as long as you provide the good arrguments.
你的问题与main()无关。您可以在代码中的任何位置调用Winapi函数,例如GetAsyncKeyState(),只要您提供良好的参数即可。
According to this list of virtual key codes the code 0x4c corresponds to the key L and not to the key K. So after a bracket correction typo in your code, I could run it succesfully interupting the loop with L
根据这个虚拟键码列表,代码0x4c对应于键L,而不是键K.因此,在代码中的括号修正拼写错误之后,我可以成功运行它,使用L替换循环
Some remarks about your function:
关于你的功能的一些评论:
Your function listentokb()
always returns 0. On the other hand, you use a global variable close
to tell the calling function the result of your keyboard scanning. This is a very bad practice: whenever possible avoid global variables.
你的函数listentokb()总是返回0.另一方面,你使用一个全局变量close来告诉调用函数键盘扫描的结果。这是一种非常糟糕的做法:尽可能避免全局变量。
Here a slightly updated version of your code that bans global variables, and use return value to communicate the results:
这里是一个略微更新的代码版本,它禁止全局变量,并使用返回值来传达结果:
const int KEY_K = 0x4B; // avoid using values directly in the code
int listentokb (void){ // returns 'K' if K is released and 0 otherwise
static int ko; // this is like a global variable: it will keep the value from one call to the other
// but it has teh advantage of being seen only by your function
if((GetAsyncKeyState(KEY_K) & 0x8000) && (ko == 0)){
ko = 1;
printf("Ok you pressed k");
return 0;
}
else if((GetAsyncKeyState(KEY_K) == 0) && (ko == 1)) {
ko = 0;
printf("Now you released it");
return 'K';
}
return 0;
}
int main(void){
bool go_on = true; // The state of the loop shall be local variable not global
while(go_on){
Sleep(50);
go_on= ! listentokb(); // if returns 0 we go on
}
return 0;
}