简体   繁体   English

打印按下的键 C++

[英]Print pressed key c++

I want to capture and print pressed key.我想捕获并打印按键。 Problem is that my program show "a" as "A" and "1" as "a".问题是我的程序将“a”显示为“A”,将“1”显示为“a”。 And i dont know how to fix.我不知道如何解决。 Will be thankfull for any help.将不胜感激任何帮助。 Cant use something like scanf or getc because it should be keylogger(consolefree) for my school project.不能使用 scanf 或 getc 之类的东西,因为它应该是我学校项目的键盘记录器(无控制台)。

My source code:我的源代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>

using namespace std;

int main(){
char c;

    while(true)
    {
        for (int i = 8; i <= 255; i++)
        {
            if (GetAsyncKeyState(i) == -32767) // I DONT UNDERSTAND THIS LINE
            {
                c = char(i);
                printf("%c", c);
            }
        }
    }

    return 0;
}

Thanks in advance for any hints or examples.在此先感谢您提供任何提示或示例。

If you take a look at this reference.如果你看看这个参考。 It states that:它指出:

The function returns 0 if key is NOT pressed and value < 0 ( less than zero) when key is currently pressed.如果未按下键,则该函数返回 0,并且当前按下键时值 < 0(小于零)。

Thus you check for a negative value in order to see if the button is pressed.因此,您检查负值以查看按钮是否被按下。 You can just as easily write it as: if (GetAsyncKeyState(i) < 0) and have it work, with the benefit of it being clearer.您可以像这样轻松地编写它: if (GetAsyncKeyState(i) < 0)并让它工作,好处是它更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM