Registered hot key "PrtScn" with RegisterHotKey WinAPI function sometimes does not work as expected

163 Views Asked by At

My application registers a hot key for PrtScr using the RegisterHotKey() WinAPI function.

In most cases, it works correctly and is caught by my application. But, if some applications become active on the screen then this hot key is not caught. At the same time, if I switch to another application, it works.

I observed such a problem in Task Manager, Chrome browser, and a few others. Sometimes it even doesn't work on one Chrome app window but works fine at the same time on another Chrome window.

I also observed the same problem with other applications which use PrtScr as a hot key, so it seems the problem is not in my application. But I still can't figure out the logic behind that.

If somebody knows what the reason is of PrtScr being blocked in such cases, I would appreciate if you share your knowledge.

1

There are 1 best solutions below

0
lex On

It should work on Chrome(unless you run Chrome with elevated privilege? no idea if it works on one window then fails on another). Failing on Task Manager is by design because of User Interface Privilege Isolation (UIPI). And with UIPI, both RegisterHotKey and LowLevelKeyboardProc will fail on task manager, or any other programs with higher privilege.

Please read Microsoft document. You can either run your app as administrator, or properly configure the UIAccess flag by signing the app and put it in a protected location.

User Interface Privilege Isolation (UIPI) implements restrictions in the Windows subsystem that prevent lower-privilege applications from sending messages or installing hooks in higher-privilege processes. Higher-privilege applications are permitted to send messages to lower-privilege processes. UIPI doesn't interfere with or change the behavior of messages between applications at the same privilege (or integrity) level.

This policy setting enforces the requirement that apps that request running with a UIAccess integrity level by marking UIAccess=true in their app manifest must reside in a secure location on the file system. Relatively secure locations are limited to the following directories:

..\Program Files\ (and subfolders)
..\Program Files (x86)\ (and subfolders, in 64-bit versions of Windows only)
..\Windows\System32\