C++ Hotkey to run function - winapi

Hey looking to assign F1 as a hotkey to run a function. I have a windows 32 project with a Form, however the form will be in the background and not the active window when I press F1.
Is there a way to code a hotkey or shortcut so when I press F1 it will run that function even if the Form1 isn't active?

Have a look at the RegisterHotKey function.
It allows you to define a system-wide hot key. When the hot key is pressed, you will receive a WM_HOTKEY message even if your window is not active.

Related

Why does RegisterHotKey succeed with Ctrl-C?

The docs for RegisterHotKey say:
RegisterHotKey fails if the keystrokes specified for the hot key have
already been registered by another hot key.
And yet registering Ctrl+C works.
Why? / How can I be notified that the hotkey is already in use (perhaps by some other function)?
EDIT
My goal it to have a user alerted to the fact that such a shortcut key is already in use and therefore they should choose a different one.
Ctrl+C is a system-defined key sequence, but it is not a pre-registered hotkey, from an application perspective. That is why an application is able to register a hotkey for it.
A hotkey is its own feature. Just because a given key sequence invokes a system action does not mean it is implemented in the system as an application-defined hotkey specifically. For instance, there is no WM_HOTKEY message generated for Ctrl+C unless it is explicitly registered by an application (WM_COPY is generated instead). Ctrl+Alt+Del is also not an application-defined hotkey either, for instance (and you can't register that).
If you want to detect/discard Ctrl+C without registering it as a hotkey, use a keyboard hook via SetWindowsHookEx().

How to change the functionality of PrtSc (PrintScreen) key?

I wrote a little program that uses the PrtSc key as a replacement for pressing / clicking the middle mouse key.
My program uses the GetAsyncKeyState API function to scan the PrtSc.
It works fine.
The problem is that the original functionality of PrtSc continues to work also.
So, as long as PrtSc is pressed, screenshots of the desktop are being made.
After a brief moment, this leads to some hickups and delays in my screen repainting.
So I would like to turn off the 'screenshot function' of PrtSc.
I tried this method of adding a registry key to change the scan code.
disable the printscreen keyboard option from windows
But this makes the PrtSc key pretty much 'disappear' for Windows.
In the sense that GetAsyncKeyState also does not see the keypresses anymore.
Does anybody know of a way to 'detach' the screenshot functionality from PrtSc, but in such a way that pressing PrtSc still generates keycodes ...?
I have a work-around for now.
Still interested in the answer to my question though...
Work-arounds (two):
I am using the Tab key instead of the PrtSc key in my program. Not entirely happy with this, since it messes with the Tab action when entering text.
Using key remapping in AutoHotkey (that I was already using)
I remap the Tab key to Middle Mouse Button, but only for the program for which I need this:
#IfWinActive "My CAD software"
*Tab::MButton
#IfWinActive

How to simulate pressing the F4 key in Swift?

I have a WkWebView browser in my application, is it possible to send an imitation of pressing the F4 button directly to it directly or in some other way?
In a similar application only on Android, I do this using the dispatchKeyEvent() method, and how can this be done in Swift?
In practice, it should look like this: the user launches the application, makes a long tap on the screen and an imitation of pressing the F4 button is triggered.
It remains for me to do an imitation of pressing the F4 key.

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.

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

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.

Resources