Run a program on: hard "Power" button press - windows

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.

Related

Windows-10-IOT QT-C++/QML App Fullscreen Gui Frozen when monitor turns back on

ISSUE:
On a touch panel with no keyboard, my QT C++/QML app running on Windows 10 IOT has the fullscreen GUI "frozen", when the monitor turns on (after the user has triggered the touchscreen), after it has timed-out earlier and turned off due to power settings. Mouse cursor still updates.
The QML GUI has "flags: Qt.FramelessWindowHint | Qt.Window"; I do not want to add "Qt.WindowStaysOnTopHint" as it will block the control panel window when it is open from the app. The program is verified to be still running, only the GUI has frozen from the point in time when the screen turned off.
TEMPORARY RESOLUTION:
The only way to "unfreeze" the fullscreen GUI is to connect a keyboard & press the Windows key to show-hide the start menu, or do it programmatically with a manual QML button placed at a known position or on detection of monitor WM_POWERBROADCAST messages.
When the app is not fullscreen, the freezing doesn't seem to be happening.
Is this due to some missing WM_MESSAGES (e.g. WM_PAINT, WM_ACTIVATE, etc) sent by the OS to the app when it is fullscreen, or when the start menu button is pressed?
Can the app-fullscreen-freezing on monitor-turn-back-on be rectified by the app programmatically sending a sequence of WM_MESSAGES to itself, but not the Win button keypress (as the normal user is not supposed to access the OS or see anything related to the OS when the app is running)?
I tried using winAPI SetForegroundWindow() function...?
:-( Fast forward a few days...
With further testing, it seems that using SetForegroundWindow() alone is not consistent/reliable. Sometimes it works, sometimes it doesn't.
The most reliable that works 99.999% of the time is still the VK_LWIN keypress sent by the app. But, as mentioned before, the app user is not supposed to see the start-menu appearing then disappearing. Best if the behavior of the VK_LWIN keypress could be duplicated to the app without seeing the start-menu...

X11 stop windows/supper key event from reaching the OS

I am making a program that locks my computer by creating a full-screen window and disabling all ways to unfocus the window. For that I need to programmatically disable/enable the Windows key:
Is there a way to catch and stop the windows key event from being passed on to the OS (Ubuntu 21.10) using libX11? From what I understand I am supposed to use XGrabKey but it only blocks Alt-Tab:
XGrabKey(display, ANY_KEY, ANY_MODIFIER, window, False, GRAB_MODE_SYNC,
GRAB_MODE_SYNC)
Also I don't want to disable the whole keyboard.

What's the MacOS API to "capture" the mouse and keyboard, the way a VM or remote desktop program would do?

I'm looking for the Mac OS API that virtual machine or remote desktop type programs would use to "capture" the mouse and keyboard. That is to say, I want to write a GUI program where when the user clicks in my window, the normal mouse cursor disappears, and all keyboard and mouse input is diverted to my program, including global shortcuts like cmd-tab. What's the name of this API?
Found it: CGEventTapCreate can tap into the low level event stream to receive, filter, or insert HID events.

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.

Running a Windows process while controlling the mouse and keyboard

I have a Windows program which has a GUI that runs on a PC.
In order to automate some of the GUI actions, I want to be able to move the mouse and type using the keyboard, but without interfering with the user's activity.
I know that I could simulate input events using SendMessage and PostMessage, but that requires the window to be in focus, and I want to eliminate this requirement.
My question is - is it possible to implement sort of a 'wrapper' that internally runs the original program, while patching its mouse and keyboard, providing it with a 'virtual' version of a mouse of keyboard?
I think of that as taking only the mouse and keyboard capabilities of a VM. Is something of that kind exists?
Thanks!

Resources