How to detect KeyPress while program is running in background in Win32 C++ - winapi

I got a program that whenever I minimize it, it goes to the system tray.
i want to know is this:
a) how could i detect a key press while my program is in the system tray.
b) how could I know what they press in the keyboard specifically the function buttons.

You need to set up a keyboard hook using SetWindowsHookEx(). Look at the WH_KEYBOARD and WH_KEYBOARD_LL hooks.

If you know exactly what keystroke you're expecting, you can use RegisterHotkey and Windows will send you a message when that key is pressed.
If you want to detect all keystrokes, #OJ's answer will work.

Related

How does on-screen (virtual) keyboard works in Win10

I haven't find anything relevant in Google or any Microsoft site about it so I decided to ask a question here.
Everybody knows that in Win-based OS there is a virtual keyboard. I also know that *nix based OS, have it too. So, the question is about:
HOW DOES IT WORK INSIDE?
I mean, let's have an example that I opened on screen keyboard in Windows 10. What's the actual difference between:
input via hardware keyboard: when I'm using it, like I press X button
..and using a virtual keyboard, when I press the same button
Imagine, I have an admin access to terminal/computer, is there any option to track/distinguish that in the second time I pressed button not on hardware keyboard, but on-screen (by mouse clicking) version of it?
And there are also many different software, like AutoIt (yes, it's a language, but it's relevant to this example) that emulating pressing the X button. How does they work in Win-based OS? Do they "in-common" with default on-screen keyboard and using the same driver/WinAPI or there is a difference between them?
And the second case, between:
default on-screen keyboard
compilated AutoIt script
..any other software that emulating press X button
I guess the only way to find out "how exactly button was pressed" is to check current processes list via taskmgr and find out have anything been launched or not. Or I'm totally wrong here, and missing something?
THE SCOPE
I have written a node.js script which emulates button pressing behaviour in windows app.
TL:DR business logic short => open notepad.exe and type `Hello world`
And could someone give me any advice/recommend any powershell/bat script (or any other solution) with demonstration of Get­Async­Key­State check behavior? With which I could easily check my own node.js script (not by functional of it, but by triggering press the X button event)
I found an answer for node.js case here: Detecting Key Presses Across Applications in Powershell
SendInput is the preferred method to generate user input in software. The Windows on-screen keyboard probably uses it for everything except Ctrl+Alt+Delete which I believe has some kind of special handling. The on-screen keyboard is only able to generate Ctrl+Alt+Delete in certain configurations.
Software-generated input is merged with normal hardware input in the RIT (Raw Input Thread) in the kernel.
A low-level keyboard hook can detect software-generated input.

Run a program on: hard "Power" button press

I want to run a program (exe file) when the Power button of my laptop is pressed. (Not when system is shutting down)
I tried getting its keycode using c# and js, but none of them capture this keypress as they only capture keyboard buttons. Look at the drop-down menu I have opened:
My problem would be solved if they add "Run a specific program..." in this drop-down:
But of course they won't add this option!
So, how do I get it done? Maybe using Task Scheduler?
There's no keycode for the power button. The driver is sitting between your OS and your hardware. When you push the "G" button on your keyboard, the driver translates that to an OS system call representing the "G" key which your program can listen for and intercept. But there's no OS system call for a representing the "power" button. Instead, your driver is translating that to OS system calls to initiate a shutdown, turn off the monitor, etc.
Your laptop driver allows you to configure which system call you want to initiate when the power button is pressed, but that driver is going to be unique to the brand and model of your laptop, and if they don't offer support for capturing that keypress through their driver, then you probably don't have any easy way to intercept it.

Event Handling of a background program

Is there a way to let my program handle events like mouse movements and key strokes although the program is working in the background?!
Example : the user is working on notepad and I want my program to handle the events happening over there.
You can install mouse and keyboard hooks.

How can my Cocoa app receive global keyboard events even if it doesn't have focus?

I'm building a little app which needs to recognize if certain keys on the keyboard were pressed. In this case the arrow keys. The app must take action when these keys get pressed, even if it's not the frontmost and has no focus.
Is this possible to do? What would I have to do to receive these keyboard events no matter where they happen?
You do this by registering a hotkey using Carbon's RegisterEventHotKey function. There are also open source libraries available that make this easier, for example SGHotKeysLib.

What message will be sent by Windows when you press "Windows+D"

I used SPY++ to hook my windows message, but there is no WM_SYSCOMMAND message was sent when I pressed "Windows+D". What message will be sent by Windows when you press "Windows+D"?
Your application will not get a window message at all. Win+D is a hotkey registered by Explorer.exe (the shell program that's responsible for showing the taskbar, desktop icons, etc.).
It simply uses RegisterHotKey and it will receive a window message when you press WIN+D and will then take care of the whole show/hide thing.
Note you can register your own WIN+x hotkeys using said function but you're not supposed to and will run into problems - unless you were replacing Explorer.exe or such.
I don't think that this is a Window-Message at all...this is most likely handled by the kernel itself.
"Windows+D" is a hotkey combination.
Perhaps you can catch it as a WM_HOTKEY message.
You can register your own hotkeys, if you want, with the RegisterHotKey function.
Windows+D is a system wide hotkey and is reserved for use by operating system.
Hotkeys with MOD_WIN can not be registered by RegisterHotKey API or received by WM_HOTKEY message

Resources