Simulate key presses in C++ - macos

I want my program to press certain keys on my keyboard without me doing it physically.
So I need a subroutine that simulates pressing any key.
What code could I use to accomplish this (Press Any Key) in C++?
(I'm creating a program for MacOSX in Xcode)
Ok I got the mouse sorted now I just need key presses

You could use low-level event functions such as CGEventPost. At a higher level, look at the Accessibility APIs, such as AXUIElementPerformAction.

Related

List of deadkeys for current keyboard

I want to show users a list of the deadkeys they can press using their current Windows language & keyboard. (As opposed to a list of possible graves, circumflexes and acutes which their keyboard doesn't operate as deadkeys.)
Grateful for help.
Keyboard layout (including dead keys) are fixed in the keyboard driver. This means your problem can be reduce to getting the setting for keyboard. Use Windows API GetLocaleInfoW() function to get the information and then create the list of dead keys for each keyboard. For your reference have a look at: Windows-keyboard-layout and GetLocaleInfoW

How to press the Fn + function keys on Windows?

In previous scripts, I have been able to use the SendKeys method to press non-alphabetic keys on my computer like the following to simulate muting,
WshShell.SendKeys(chr(&hAD)).
However, I cannot seem to find the ability to simulate pressing Fn and any of the F1-F12 keys. I am trying specifically to press the "Lower Screen Brightness" key, which is Fn+F11 on my keyboard.
How can this be accomplished?
The Fn keys on a laptop are usually implemented at the hardware or driver level. See: the answer to a similar question: How to press Fn+F11
Your best bet is to find a method of performing the action directly from the OS rather than trying to press a key that doesn't 'exist' as far as Windows is concerned. In the case of screen brightness, there are other ways to do that. This solution is written in C# but if you really needed to do it in VBScript, you could potentially write a COM or DLL to handle it.

How does TranslateAccelerator know about CTRL or SHIFT modifier?

I'm working in a program that uses Accelerator keys for user-defined hot keys, and every thing works fine. The user can set hotkeys using SHIFT, CTRL, or ALT. I know that using ALT generates a WM_SYSKEYDOWN rather than WM_KEYDOWN, so it's pretty obvious when ALT is held down.
TranslateAccelerator only takes a window handle, the accel table handle and a single KEYDOWN message. So, my question is, if the user presses CTRL+T, how does TranslateAccelerator know the CTRL key was also pressed?
I know that the CTRL generates a separate KEYDOWN command, and I specifically filtered those out (do not pass to TranslateAccelerator) to test a theory, but TranslateAccelerator is still working.

Remap Caps lock key to Esc in Mma 7

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?
I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.
Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.
I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...
Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]
but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.
Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.
edit: I did not see you say which operating system you had, so I've added Mac instructions.
Windows
For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:
How can a hotkey or hotstring be made exclusive to certain program(s)?
In other words, I want a certain key to act as it normally does except when a specific window is active.
In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":
$NumpadEnter::
IfWinNotActive, CAD Editor
{
Send, {NumpadEnter}
return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.
~NumpadEnter::
IfWinNotActive, CAD Editor
return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return
To quote from "MRCS" in this forum post, you may find the following useful:
The first one I named CapsLockR.ahk and contains the following script:
CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk
The second one is named CapsLock.ahk and has this script:
GetKeyState, state, CapsLock, T
if state = D
SetCapsLockState, off
else
SetCapsLockState, on
exit
Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.
Linux
I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)
Mac
Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...
If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

Redefining keys in Emacs on Windows

I use my command keys as extra control keys on Mac OS X (and I believe the space cadet keyboards had this configuration). I want to emulate this on my Windows machines by switching Alt to Ctrl and the Windows key to Alt within Emacs. Is this possible? I found this post which suggests something like
(setq w32-pass-lwindow-to-system nil
w32-pass-rwindow-to-system nil
w32-pass-apps-to-system nil
w32-lwindow-modifier 'super ;; Left Windows key
w32-rwindow-modifier 'super ;; Right Windows key
w32-apps-modifier 'hyper) ;; Menu key
to get super and hyper keys upon pressing the windows key. When I try to remap this to 'control to test it out (but eventually I want it to be meta and Alt as Ctrl, as mentioned), windows-e still gets intercepted by Windows (XP) and opens Explorer, but seems like a good place to start? I would appreciate any suggestions.
From the documentation for w32-pass-lwindow-to-system:
Note that some combinations of the left "Windows" key with other keys are
caught by Windows at low level, and so binding them in Emacs will have no
effect. For example, <lwindow>-r always pops up the Windows Run dialog,
<lwindow>-<Pause> pops up the "System Properties" dialog, etc. However, see
the doc string of `w32-phantom-key-code'.
From the documentation for w32-phantom-key-code:
Virtual key code used to generate "phantom" key presses.
Value is a number between 0 and 255.
Phantom key presses are generated in order to stop the system from
acting on "Windows" key events when `w32-pass-lwindow-to-system' or
`w32-pass-rwindow-to-system' is nil.
And as it also says on Xah's website:
There's no way around that unless you use other tools such as AutoHotkey. Even so, it may not be possible to disable 【Win+l】 (lock window) and 【Win+u】 (easy access).
So it seems that if it doesn't work with the method he describes (which seems right), you should look into AutoHotKey.

Resources