How to convert ASCII character to CGKeyCode?

This is what I ended up using. Much cleaner. #include <CoreFoundation/CoreFoundation.h> #include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */ /* Returns string representation of key, if it is printable. * Ownership follows the Create Rule; that is, it is the caller’s * responsibility to release the returned object. */ CFStringRef createStringForKey(CGKeyCode keyCode) { … Read more

Differentiate between mouse and keyboard triggering onclick

Could check if event.screenX and event.screenY are zero. $(‘a#foo’).click(function(evt) { if (evt.screenX == 0 && evt.screenY == 0) { window.alert(‘Keyboard click.’); } else { window.alert(‘Mouse click.’); } }); Demo on CodePen I couldn’t find a guarantee that it works in all browsers and all cases, but it has the benefit of not trying to detect … Read more

jQuery: how to filter out non-character keys on keypress event?

The selected answer for this question is not complete. It does not handle the case where a character key is being pressed in combination with a modifier key (e.g. CTRL–A). Try, for example, typing CTRL–A using firefox with the following code. The current answer will consider it as a character: HTML: <input placeholder=”Try typing CTRL-A … Read more

Python simulate keydown

This code should get you started. ctypes is used heavily. At the bottom, you will see example code. import ctypes LONG = ctypes.c_long DWORD = ctypes.c_ulong ULONG_PTR = ctypes.POINTER(DWORD) WORD = ctypes.c_ushort class MOUSEINPUT(ctypes.Structure): _fields_ = ((‘dx’, LONG), (‘dy’, LONG), (‘mouseData’, DWORD), (‘dwFlags’, DWORD), (‘time’, DWORD), (‘dwExtraInfo’, ULONG_PTR)) class KEYBDINPUT(ctypes.Structure): _fields_ = ((‘wVk’, WORD), (‘wScan’, … Read more

Key Presses in Python

Install the pywin32 extensions. Then you can do the following: import win32com.client as comclt wsh= comclt.Dispatch(“WScript.Shell”) wsh.AppActivate(“Notepad”) # select another application wsh.SendKeys(“a”) # send the keys you want Search for documentation of the WScript.Shell object (I believe installed by default in all Windows XP installations). You can start here, perhaps. EDIT: Sending F11 import win32com.client … Read more

How do I capture a key press (or keydown) event on a div element?

(1) Set the tabindex attribute: <div id=”mydiv” tabindex=”0″ /> (2) Bind to keydown: $(‘#mydiv’).on(‘keydown’, function(event) { //console.log(event.keyCode); switch(event.keyCode){ //….your actions for the keys ….. } }); To set the focus on start: $(function() { $(‘#mydiv’).focus(); }); To remove – if you don’t like it – the div focus border, set outline: none in the CSS. … Read more