@@ -41,17 +41,40 @@ private String getString(int id) {
41
41
return game .getResources ().getString (id );
42
42
}
43
43
44
+ private static byte getModifier (short key ) {
45
+ switch (key ) {
46
+ case KeyboardTranslator .VK_LSHIFT :
47
+ return KeyboardPacket .MODIFIER_SHIFT ;
48
+ case KeyboardTranslator .VK_LCONTROL :
49
+ return KeyboardPacket .MODIFIER_CTRL ;
50
+ case KeyboardTranslator .VK_LWIN :
51
+ return KeyboardPacket .MODIFIER_META ;
52
+
53
+ default :
54
+ return 0 ;
55
+ }
56
+ }
57
+
44
58
private void sendKeys (short [] keys ) {
59
+ final byte [] modifier = {(byte ) 0 };
60
+
45
61
for (short key : keys ) {
46
- conn .sendKeyboardInput (key , KeyboardPacket .KEY_DOWN , (byte ) 0 );
62
+ conn .sendKeyboardInput (key , KeyboardPacket .KEY_DOWN , modifier [0 ]);
63
+
64
+ // Apply the modifier of the pressed key, e.g. CTRL first issues a CTRL event (without
65
+ // modifier) and then sends the following keys with the CTRL modifier applied
66
+ modifier [0 ] |= getModifier (key );
47
67
}
48
68
49
69
new Handler ().postDelayed ((() -> {
50
70
51
71
for (int pos = keys .length - 1 ; pos >= 0 ; pos --) {
52
72
short key = keys [pos ];
53
73
54
- conn .sendKeyboardInput (key , KeyboardPacket .KEY_UP , (byte ) 0 );
74
+ // Remove the keys modifier before releasing the key
75
+ modifier [0 ] &= ~getModifier (key );
76
+
77
+ conn .sendKeyboardInput (key , KeyboardPacket .KEY_UP , modifier [0 ]);
55
78
}
56
79
}), KEY_UP_DELAY );
57
80
}
0 commit comments