I am trying to figure out how to make that write out the keys i am typing. How can I accomplish this?

#include <windows.h>
#include <stdio.h>

main()
{
while(1){
 for(short character=8;character<=222;character++)

 if(GetAsyncKeyState(character)==-32767)
    printf("%d : %c\n",character,character);
};

Recommended Answers

All 16 Replies

What exactly do you want to do? You want to capture every key that is being pressed, create a file with user input, or something else?

What exactly is this for?

Record all the keystrokes i made. So if I type a word doc and doesn't save it will be in that fille.

I tried something with fstream it ended up gibing me a bunch of errors. i also dont know how often i should get it to add to the file.

First google, how to capture the keys, and then fstream is really easy:

ifstream input;
input.open("data.ini", fstream::in | fstream::out | fstream::app); //data.txt or any /other name or extension is ok
input >> keys;

I am sure there is a way to capture keys in Windows, you just have to google it.

And when you have the function you can do something like.

while(true) {
char key = windowsgetkeypressfunction();
input >> keys >> "    ";
}

Isn't what I am doing capturing the keys?

i just realised that this would not work on mac. is there any global way like mac pc and linux to capture the keys?

Sorry, I did not pay attention, that you already had some of the code. And there is no global way to capture the keys. Standard C++ defines only basic things. On some Operating Systems there is no MOUSE so there is not global way to capture the keys. You can use conditional compilation like but you will still have to have functions for different operating systems.

oh so do you know how i could put all of the stuff together because i think my code does capture keys correctly. now i need to make it into a file. i just dont know how to do it. i dont really understand fstream

It's really easy. Follow my example. Assuming you already have the keys you want to write to the file:

ifstream input;
input.open("data.ini", fstream::in | fstream::out | fstream::app);
input >> keys;

Also you have to include fstream.h

so it should be?

#include <windows.h>
#include <stdio.h>

main()
{
while(1){
 for(short character=8;character<=222;character++)

 if(GetAsyncKeyState(character)==-32767)
    
ifstream input;
input.open("data.ini", fstream::in | fstream::out | fstream::app);
input >> keys;
};

actually i think it should be

#include <windows.h>
#include <stdio.h>
#include <fstream.h>
main()
{
ifstream input;
input.open("data.ini", fstream::in | fstream::out | fstream::app);
while(1){
 for(short character=8;character<=222;character++)

 if(GetAsyncKeyState(character)==-32767)
    

input >> keys;
};

I think the op wants to write to the file. So use ofstream.

ofstream input;
input.open("data.ini", fstream::in | fstream::out | fstream::app);
input<<keys;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.