xcb keyboard button masks meaning - x11

I'm trying to figure what keys are handled by:
XCB_MOD_MASK_1
XCB_MOD_MASK_2
XCB_MOD_MASK_3
XCB_MOD_MASK_4
XCB_MOD_MASK_5
in xcb, for XCB_MOD_MASK_1 it seems to be Alt (i'm correct?), but for others button i cannot find the mapping anywhere (i tried to google them before posting, but with no success).
So what are the usual key associated to these masks?

Usually Mask1 is Alt or Meta, Mask2 is Num lock, Mask3 is AltGr, Mask4 is Win, and Mask5 is Scroll lock, but this varies between X implementations and/or keyboard models.
Source: my own computer running X11, and various bits and pieces of code lying around on the 'net. Not all of them are consistent, e.g. some say Mod1 is Alt and Mod4 is Meta.
X11 programs normally let users configure actions corresponding to Mask1 ... Mask5, and have them sort out which key sets which mask.

You can actually configure what those keys (or any keys) are mapped to, with a utility like xmodmap. In the X Window System there are eight modifiers. The 8 modifiers are
shift
lock
control
mod1
mod2
mod3
mod4
mod5
To see what keys are currently mapped to these modifiers you can run xmodmap -pm, which prints the modifier map. Eg. for me the output is
xmodmap: up to 4 keys per modifier, (keycodes in parentheses):
shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x69)
mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf)
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
So Alt generates mod1, for instance.
Now to change mod1 through mod5 with xmodmap, open ~/.Xmodmap and write something like:
clear mod1
clear mod2
clear mod3
clear mod4
clear mod5
add mod1 = Alt_L Alt_R Meta_L
add mod2 = Num_Lock
add mod3 = ISO_Level3_Shift
add mod4 = Super_L Super_R Super_L Hyper_L
add mod5 = ISO_Level5_Shift
Then run xmodmap ~/.Xmodmap.
And now, for instance, ISO_Level3_Shift is what gives you mod3.
How you can actually get a key from your keyboard to generate the keycode that corresponds to ISO_Level3_Shift is another challenge.
Eg. to get keycode 100 to generate ISO_Level3_Shift (which is now mod3), add the following to your ~/.Xmodmap file and run xmodmap ~/.Xmodmap.
keycode 100 = ISO_Level3_Shift
The keycode with, for instance, value 100 should be generated by udev. When you press a physical key in your keyboard, the microcontroller/firmware maps that physical key to a scancode and sends that scancode through the USB interface. Then udev maps that scancode to a Linux kernel keycode. Then X11 maps that kernel keycode to an X11 keycode. And then X11 maps that X11 keycode to an X11 keysym. Needless to say, this mapping is unnecessarily complicated. (In particular, I almost always ignore X11 keysyms and work with raw X11 keycodes.)
You might hear that xmodmap is deprecated, and that you should mess with XKB config files and stuff, but using XKB is much, much, much worse.
(Keyboards are really simple (just a circuit+microcontroller that scans the physical key matrix, keyboard-side firmware that knows USB HID, and then a kernel-side driver that maps firmware-dependent scancodes to standardized keycodes), but people have made them really complicated. Also, scancodes/keycodes should've been 32-bit from the start.)
In the X protocol, the 8 modifiers (shift, ..., mod1, ..., mod5) have a bitmask associated to them. This is the bitmask that XCB implements using a C enum, and its precise values are:
enum xcb_mod_mask_t{
XCB_MOD_MASK_SHIFT = 1<<0,
XCB_MOD_MASK_LOCK = 1<<1,
XCB_MOD_MASK_CONTROL = 1<<2,
XCB_MOD_MASK_1 = 1<<3,
XCB_MOD_MASK_2 = 1<<4,
XCB_MOD_MASK_3 = 1<<5,
XCB_MOD_MASK_4 = 1<<6,
XCB_MOD_MASK_5 = 1<<7,
XCB_MOD_MASK_ANY = 1<<15,
};
These values aren't XCB's choice, but they're prescribed by the X protocol specification and you can use them when talking to the X server through the X protocol. Eg. when the X server sends you an XCB_KEY_PRESS event, this event is a 32-byte struct, and one of its fields is a bitmask where the bits are set according to the modifiers that were pressed during that key press event. Eg. if bit 0 is set, it means XCB_MOD_MASK_SHIFT is set, meaning the Shift modifier was help down.

Related

Injecting key combinations into Bash tty using TIOCSTI in Python

I am trying to inject key combinations (like ALT+.) into a tty using the TIOCSTI in Python.
For some key combinations I have found the corresponding hex code for Bash shells using the following table which works good.
From this table I can see that for example CTRL+A is '\x01' etc.
import sys,os,Queue
import termios,fcntl
# replace xx with a tty num
tty_name = "/dev/pts/xx";
parent_fd = os.open(tty_name, os.O_RDWR)
special_char = "Ctrl_a"
if special_char == "Ctrl_a":
send_char = '\x01'
if special_char == "Ctrl_e":
send_char = '\x05'
if special_char == "Ctrl_c":
send_char = '\x03'
fcntl.ioctl(self.parent_fd, termios.TIOCSTI, send_char)
But how can I get the hex codes for other combinations such as
ALT+f etc. I need a full list or a way how to get this information for any possible combo as I want to implement most bash shortcuts for moving, manipulating the history etc. to inject.
Or is there any other way to inject key-combinations using TIOCSTI ?
As I can only send single chars to a tty I wonder if there is anything else possible.
Thank you very much for your help!
The usual working of "control codes" is that the "control" modifier substracts 64 from the character code.
"A" is ASCII character 65, so "Ctrl-A" is "65-64=1".
Is it enough for you to extend this scheme to your situation?
So, if you need the control code for, for example, "Device Control 4" (ASCII code 20), you'd add 64, to obtain "84", which is "T".
Therefore, the control-code for DC4 would be "Control+T".
In the reverse direction, the value for "Control+R" (history search in BASH) is R-64, so 82-64=18 (Device Control 2)
ASCIItable.com can help with a complete listing of all character codes in ASCII
Update: Since you were asking specifically for "alt+.":
The 'Control mean minus 64" doesn't apply to Alt, unfortunately; that seems to be handled completely differently, by the keyboard driver, by generating "key codes" (also called "scancodes", variably written with or without spaces) that don't necessarily map to ASCII. (Keycodes just happen to map to ASCII for 0-9 and A-Z, which leads to much confusion)
This page lists some more keycodes, including "155" for "alt+."

Keyboard hack on an OSX Mac to change bad typing habits

I had an idea (not sure whether it's original) that will force me to use the Shift keys correctly.
Is it possible for me to make the left hand shift key disable all other LH keys, and the right hand shift key disable all other RH keys, so that in order to get a !##$% or ASDF, etc, I am forced to use the RH shift key, while in order to get &*() or JKL:, etc, I am forced to use the LH shift key?
I have gotten in the habit of capitalizing EVERYTHING with the LH pinky, and it's starting to hurt. I need a profound and radical retraining!
There is a utility called Karabiner (KeyRemap4Macbook) which looks promising.
https://pqrs.org/osx/karabiner/index.html

In OS X (lion) how can I find whether a key combination is in use as a keyboard shortcut?

I want to put a bunch of keyboard shortcuts in my app (OS X lion) so I can do most things from the keyboard. There are of course a bunch of lists of hot key combos in use already, including the one in the HIG.
Is there some utility that can be used to type a key combination and find out if it already means something (either globally, or mac standard -- I'm not too worried about reusing some special combo used by another app -- or should I be?)?
You can use Carbon to do this. Don't be afraid to use Carbon here, there is no Cocoa way to get this information and the Carbon methods are still supported.
The CopySymbolicHotKeys() function returns an array of dictionaries, containing information about the system-wide symbolic hot keys defined in the Keyboard preferences pane. Each dictionary contains information about a single hot key.
Specifically, each dictionary has three keys:
kHISymbolicHotKeyCode: The virtual key code of the hot key, represented as a CFNumber.
kHISymbolicHotKeyModifiers: The hot key’s keyboard modifiers, represented as a CFNumber.
kHISymbolicHotKeyEnabled: The enabled state of the hot key, represented as a CFBoolean.
Obviously these are raw key codes so you will need to do some work if you want to see what the key codes actually refer to.
Note that the array doesn't contain custom, application-specific hotkeys, but this is a minor problem.
Here's a simple example:
#import <Carbon/Carbon.h>
CFArrayRef registeredHotKeys;
if(CopySymbolicHotKeys(&registeredHotKeys) == noErr)
{
CFIndex count = CFArrayGetCount(registeredHotKeys);
for(CFIndex i = 0; i < count; i++)
{
CFDictionaryRef hotKeyInfo = CFArrayGetValueAtIndex(registeredHotKeys, i);
CFNumberRef hotKeyCode = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyCode);
CFNumberRef hotKeyModifiers = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyModifiers);
CFBooleanRef hotKeyEnabled = CFDictionaryGetValue(hotKeyInfo, kHISymbolicHotKeyEnabled);
NSLog(#"key code: %# modifiers: %# enabled: %#", hotKeyCode, hotKeyModifiers, hotKeyEnabled);
}
//you MUST release the dictionary when finished with it
CFRelease(registeredHotKeys);
}
Remember that you'll need to add the Carbon framework to the Link Binary with Libraries build phase in your project settings.
For more information you should look at the Carbon Event Manager docs (11Mb PDF).
Thear used to be an API in Carbon to get the global keyboard shortcuts, however, I do not believe there is a Cocoa API for this. I don't think you should worry about other third party apps, but you could refer to http://support.apple.com/kb/HT1343 and just hard code to avoid those. He that helps.

Identify key uniquely from WM_KEYDOWN message

I tried to use the virtual key code provided by wParam, however that didn't work very well:
multiple keys mapped to the same key code
some keys were not recognized at all
virtual keys seemed to be adapted to the keyboard layout (which i don't want/need)
Then i saw that the lParam will give me a scancode along with an "extended" flag, which seem to produce a different value for every single key on the keyboard when calculated like this:
value = (lParam & 0x01FF0000) >> 16;
Will this value always be identical for the same key on the keyboard, even across various keyboards/systems?
Scancodes can be different for different keyboards. Best to use virtual key codes. From http://msdn.microsoft.com/en-us/library/ms646267(v=vs.85).aspx:
Assigned to each key on a keyboard is a unique value called a scan
code, a device-dependent identifier for the key on the keyboard. A
keyboard generates two scan codes when the user types a key—one when
the user presses the key and another when the user releases the key.
The keyboard device driver interprets a scan code and translates
(maps) it to a virtual-key code, a device-independent value defined by
the system that identifies the purpose of a key.

KeyPress event in Lua?

is it possible to get users keypress on lua?
fe.
while true do
if keyPress(27)==true then
print("You just pressed ESC")
end
end
Lua is predicated on extreme portability. As such it's based on supplying, essentially, only that which is available in ANSI C in terms of capabilities. (I think the sole exception to that is dynamic linking which is a non-ANSI feature not available on all platforms, but is so useful that they've put it in for many.)
ANSI C doesn't provide keypress functionality so the default Lua library doesn't either.
That being said, the LuaRocks repository might lead you to a library with this capability. For example it could be that ltermbox, found on the LuaRocks page there, has the functionality you need. (You'll probably have to remove the bits you don't want, mind.) There may be other libraries available. Go digging.
Failing that, the whole point of Lua is extensibility. It's an extensible extension language. It's not actually all that hard to hand-roll your own extension that provides the functionality you want.
Not in stock Lua. Probably with an additional library.
There is a binding to getkey() in the NTLua project. You can get some sources from there.
(it just wraps getch())
It seems like you are trying to make a game. For 2D games you might want to consider love2d. It looks a little weird, but it works and it's relatively easy compared to other languages such as C.
First thing's first: if you're using my method of doing this, you need to put the script(s) you use in a LocalScript. Not doing this will cause the key(s) to not show up in the console (F9 to see console).
Alright, now that we know it's in a LocalScript, here's the script:
local player = game.Players.LocalPlayer -- Gets the LocalPlayer
local mouse = player:GetMouse() -- Gets the player's mouse
mouse.KeyDown:connect(function(key) -- Gets mouse, then gets the keyboard
if key:lower() == "e" or key:upper() == "E" then -- Checks for selected key (key:lower = lowercase keys, key:upper = uppercase keys)
print('You pressed e') -- Prints the key pressed
end -- Ends if statement
end) -- Ends function
If you're wanting to signal only one key (lowercase only, or uppercase only) check below.
Lowercase only:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if key == "e" then
print('You pressed e')
end
end)
Uppercase only:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
if key == "E" then
print('You pressed E')
end
end)
Or, if you want to just signal any key in general, you can also do this:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
print('You pressed '..key)
end)
I hope I helped answer your question.
if keypress=(29)==true then
print("hello")
end

Resources