No idea; I’m not a Mac user.
Let’s first explain why I use the hasAlt
, hasCtrl
and hasShift
flags. The variables key
and keyCode
reflect the last change. So if you press e.g. the alt key followed by the ‘a’ key (while keeping the alt key pressed), the system will only give you the ‘a’ key.
The below code displays the values for the keyCode and the key when a key (or key combination) is pressed. With this you can follow the sequence of what is happening.
boolean hasAlt = false;
boolean hasCtrl = false;
boolean hasShift = false;
void setup()
{
// give the window the focus
surface.setVisible(true);
}
void draw()
{
}
void keyPressed()
{
println(millis() + "\t[keyPressed()]keyCode = " + hex(keyCode) + ", key = " + hex(key));
//printKey("[keyPressed()]");
if (key == CODED)
{
switch(keyCode)
{
case ALT:
hasAlt = true;
break;
case CONTROL:
hasCtrl = true;
break;
case SHIFT:
hasShift = true;
break;
default:
break;
}
}
}
void keyReleased()
{
println(millis() + "\t[keyReleased()]keyCode = " + hex(keyCode) + ", key = " + hex(key));
//printKey("[keyReleased()]");
if (key == CODED)
{
switch(keyCode)
{
case ALT:
hasAlt = false;
break;
case CONTROL:
hasCtrl = false;
break;
case SHIFT:
hasShift = false;
break;
}
}
}
Some output examples. A key value of 0xFFFF indicates a coded key.
1a)
2752 [keyPressed()]keyCode = 0000005A, key = 007A // 'z' key pressed
2851 [keyReleased()]keyCode = 0000005A, key = 007A // 'z' key released
In this example you can see that the keyCode 0x5A (ascci code for capital ‘Z’) and the key is 0x7A (ascii code for lower case ‘z’).
1b)
5752 [keyPressed()]keyCode = 00000010, key = FFFF // shift key pressed
5969 [keyPressed()]keyCode = 0000005A, key = 005A // 'z' key pressed
6051 [keyReleased()]keyCode = 0000005A, key = 005A // 'z' key released
6352 [keyReleased()]keyCode = 00000010, key = FFFF // shift key released
The easy way to differentiate between capital ‘Z’ and lowercase ‘z’ is by testing the key directly
if(key == 'Z') // 0x5A
{
}
if(key == 'z') // 0x7A
{
}
The difficult way is to check the hasShift
flag and the keyCode.
if(keyCode == 0x5a) // or if(keyCode == 'Z')
{
// lowercase 'z'
if(hasShift == false)
{
}
// capital 'Z'
else
{
}
}
2a)
4674 [keyPressed()]keyCode = 00000011, key = FFFF // control key pressed
4941 [keyPressed()]keyCode = 00000041, key = 0001 // 'a' key pressed
5075 [keyReleased()]keyCode = 00000041, key = 0001 // 'a' key released
5241 [keyReleased()]keyCode = 00000011, key = FFFF // control key released
You can see that ‘a’ was pressed based on the keyCode; 0x41 is the ascii code for capital ‘A’. Because of special meanings of a number of control keys (<ctrl>A
to <ctrl>Z
) the key is now 0x01.
The easy way to determine if <ctrl>A
is pressed is to check the key
if(key == 0x01) // ctrl-A
{
}
The difficult way is similar as the difficlt way above.
2b)
9139 [keyPressed()]keyCode = 00000012, key = FFFF // alt key pressed
9424 [keyPressed()]keyCode = 00000041, key = 0061 // 'a' key pressed
9557 [keyReleased()]keyCode = 00000041, key = 0061 // 'a' key released
9922 [keyReleased()]keyCode = 00000012, key = FFFF // alt key released
The only way to determine if the key is <alt>a
is to use the hasAlt
flag and either the keyCode or the key
if(hasAlt == true)
{
if(key == 0x61) // lowercase 'a'
{
}
if(key == 0x41) // capital 'A'
{
}
}
3)
2127 [keyPressed()]keyCode = 00000027, key = FFFF // right arrow key pressed
2212 [keyReleased()]keyCode = 00000027, key = FFFF // right arrow key released
9861 [keyPressed()]keyCode = 00000011, key = FFFF // control key pressed
9977 [keyPressed()]keyCode = 00000027, key = FFFF // right arrow key pressed
10027 [keyReleased()]keyCode = 00000027, key = FFFF // right arrow key released
10227 [keyReleased()]keyCode = 00000011, key = FFFF // control key released
13178 [keyPressed()]keyCode = 00000012, key = FFFF // alt key pressed
13311 [keyPressed()]keyCode = 00000027, key = FFFF // right arrow key pressed
13378 [keyReleased()]keyCode = 00000027, key = FFFF // right arrow key released
13595 [keyReleased()]keyCode = 00000012, key = FFFF // alt key released
0x27 is the keyCode for the right cursor key. In this example, all keys are coded keys (hence 0xFFFF) so you don’t have a means to differentiate between <right>
, <ctrl><right>
and <alt><right>
on the key or the keyCode. You will have to use hasCtrl
and hasAlt
in combination with the keyCode.