I'm trying to get the key being pressed in order to start an algorithm and afterwards move the mouse on the screen and perform some clicks. How can I achieve this when the console is in the taskbar ?

I tried _getch() but it doesn't work while the console is minimised.
I tried searching on google but found nothing.

Recommended Answers

All 9 Replies

You'll need to hook the keyboard at a low level to manage this. I'd suggest studying software key loggers for ideas. However, generally this kind of functionality is either unnecessary or malicious. Why do you want to process key strokes when the program is inactive?

For automating tasks on my computer. I know there is software I can download but I want to do this myself since I'm learning C++.

Could you point me to some source code I have no idea where to start ?

Google for things like "keylogger source C++", or "global Windows hook C++". I don't personally have any code that I want to share, but the web is full of examples if you take the time to look around.

commented: Thanks ! +1

Well, I pretty much found what I needed, never even crossed my mind to search for keyloggers, those are supposed to be frowned upon for being related to viruses and illegal activity. I guess they can have some good uses too :)

Thanks

If you fail to go for the hook, look into
I can help you with 2 API functions:

ShowWinow(HWND, bool); //use false for 2nd arguement to hide
GetAsyncKeyState();    //U'll have to AND it with the v_Key
if(GetAsyncKeyState('A')&1)
    //Do somework

But LLKBHooks are the best.

That's the function I found "GetAsyncKeyState()", but it's rather slow. I mean I have to keep a button pressed for 2-3 seconds get it. Why is that ?

I'm looking into these hooks, this is some serious stuff, writing dlls and using includes from mfc. Maybe I can find a simpler example though.

That's the function I found "GetAsyncKeyState()", but it's rather slow. I mean I have to keep a button pressed for 2-3 seconds get it. Why is that ?

No, you don't have to keep it pressed for that long. try using Sleep(100) b/w loop.
And if you've just started MFC and are not familiar with callback functions, writing a hook procedure will be difficult for you.

commented: Great ! +1

Thanks, it works !

Glad to be of help. BTW you should be know the virtual key codes
A-Z & 0-9 are all chars 'A'-Z' & '0'-'9'
rest are here: Virtual Key Codes

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.