Modifier Keys


An integer bit field indicating the modifier keys in effect. You can examine individual flag settings using the C bitwise AND operator.

65536 NSAlphaShiftKeyMask

Set if Caps Lock key is pressed.

131072 NSShiftKeyMask

Set if Shift key is pressed.

262144 NSControlKeyMask

Set if Control key is pressed.

524288 NSAlternateKeyMask

Set if Option or Alternate key is pressed.

1048576 NSCommandKeyMask

Set if Command key is pressed.

2097152 NSNumericPadKeyMask

Set if any key in the numeric keypad is pressed. The numeric keypad is generally on the right side of the keyboard.

4194304 NSHelpKeyMask

Set if the Help key is pressed.

8388608 NSFunctionKeyMask

Set if any function key is pressed. The function keys include the F keys at the top of most keyboards (F1, F2, and so on) and the navigation keys in the center of most keyboards (Help, Forward Delete, Home, End, Page Up, Page Down, and the arrow keys).

arrow keys have an extra modifier value of 10485760

so for a shift arrow key do a bitwise 'or' with the shift mask:

10485760 | 131072

= 10616832 // this is the mask for shift arrow key


Three usage examples, direct and with helper methods

isCaps, isShift, isCtrl, isAlt, isCmd, isNumPad, isHelp, isFun:


(

w = Window.new.front; w.addFlowLayout;


// direct, using bit field

Button(w, Rect(0,0,390,50)).states_([["hold shift/alt keys and click"]])

.action_ { |but, mod| 

if (mod & 131072 == 131072) { "shift key was pressed.".postln; };

if (mod & 524288 == 524288) { "alt key was pressed.".postln; };

};


// using helper methods

Button(w, Rect(0,0,390,50)).states_([["hold any mod keys and click"]])

.action_ { |but, mod| 

if(mod.isCaps) { "mod pressed: Caps.".postln };

if(mod.isShift) { "mod pressed: Shift.".postln };

if(mod.isCtrl) { "mod pressed: Ctrl.".postln };

if(mod.isAlt) { "mod pressed: Alt.".postln };

if(mod.isNumPad) { "mod pressed: NumPad.".postln };

if(mod.isHelp) { "mod pressed: Help.".postln };

if(mod.isFun) { "mod pressed: Fun.".postln };

};


// collecting all modifiers

Button(w, Rect(0,0,390,50)).states_([["hold any mod keys and click"]])

.action_ { |but, mod| 

var pressed;

#[\isCaps, \isShift, \isCtrl, \isAlt, \isCmd, \isNumPad, \isHelp, \isFun].do { |x|

if(mod.perform(x)) { pressed = pressed.add(x) }

};

pressed.postln;

};

)