Fix iOS/macOS keycode handling regression for textoverlay example, update and use keycodes.hpp defines for iOS/macOS

This commit is contained in:
Stephen Saunders 2022-05-13 21:57:30 -04:00
parent 8080b7a91b
commit a1e19ea5de
5 changed files with 43 additions and 49 deletions

View file

@ -40,11 +40,12 @@
#define GAMEPAD_BUTTON_START 0x1006 #define GAMEPAD_BUTTON_START 0x1006
#define TOUCH_DOUBLE_TAP 0x1100 #define TOUCH_DOUBLE_TAP 0x1100
#elif defined(VK_USE_PLATFORM_IOS_MVK) #elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
// Use numeric keys instead of function keys. #if !defined(VK_EXAMPLE_XCODE_GENERATED)
// Use main keyboard plus/minus instead of keypad plus/minus // For iOS and macOS pre-configured Xcode example project: Use character keycodes
// Use Delete key instead of Escape key. // - Use numeric keys instead of function keys.
#define KEY_ESCAPE 0x33 #define KEY_DELETE 0x7F
#define KEY_ESCAPE 0x1B
#define KEY_F1 '1' #define KEY_F1 '1'
#define KEY_F2 '2' #define KEY_F2 '2'
#define KEY_F3 '3' #define KEY_F3 '3'
@ -62,14 +63,18 @@
#define KEY_L 'l' #define KEY_L 'l'
#define KEY_N 'n' #define KEY_N 'n'
#define KEY_O 'o' #define KEY_O 'o'
#define KEY_Q 'q'
#define KEY_T 't' #define KEY_T 't'
#define KEY_Z 'z'
#elif defined(VK_USE_PLATFORM_MACOS_MVK) #else // defined(VK_EXAMPLE_XCODE_GENERATED)
// For compatibility with iOS UX and absent keypad on MacBook: // For cross-platform cmake-generated Xcode project: Use ANSI keyboard keycodes
// - Use numeric keys instead of function keys // - Use numeric keys instead of function keys
// - Use main keyboard plus/minus instead of keypad plus/minus // - Use main keyboard plus/minus instead of keypad plus/minus
// - Use Delete key instead of Escape key // Note: Carbon kVK_* definitions are used instead of these in vulkanexamplebase.cpp
#define KEY_ESCAPE 0x33 // Currently only KEY_SPACE and KEY_KPADD are used in textoverlay.cpp example
#define KEY_DELETE 0x33
#define KEY_ESCAPE 0x35
#define KEY_F1 0x12 #define KEY_F1 0x12
#define KEY_F2 0x13 #define KEY_F2 0x13
#define KEY_F3 0x14 #define KEY_F3 0x14
@ -87,7 +92,10 @@
#define KEY_L 0x25 #define KEY_L 0x25
#define KEY_N 0x2D #define KEY_N 0x2D
#define KEY_O 0x1F #define KEY_O 0x1F
#define KEY_Q 0x0C
#define KEY_T 0x11 #define KEY_T 0x11
#define KEY_Z 0x06
#endif
#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
#define KEY_ESCAPE DIKS_ESCAPE #define KEY_ESCAPE DIKS_ESCAPE

View file

@ -1601,6 +1601,7 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
case kVK_ANSI_P: case kVK_ANSI_P:
vulkanExample->paused = !vulkanExample->paused; vulkanExample->paused = !vulkanExample->paused;
break; break;
case kVK_Delete: // support keyboards with no escape key
case kVK_Escape: case kVK_Escape:
[NSApp terminate:nil]; [NSApp terminate:nil];
break; break;
@ -1617,6 +1618,7 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
vulkanExample->camera.keys.right = true; vulkanExample->camera.keys.right = true;
break; break;
default: default:
vulkanExample->keyPressed(event.keyCode); // handle example-specific key press events
break; break;
} }
} }
@ -1633,9 +1635,9 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
break; break;
case kVK_ANSI_A: case kVK_ANSI_A:
vulkanExample->camera.keys.left = false; vulkanExample->camera.keys.left = false;
break; break;
case kVK_ANSI_D: case kVK_ANSI_D:
vulkanExample->camera.keys.right = false; vulkanExample->camera.keys.right = false;
break; break;
default: default:
break; break;

View file

@ -21,11 +21,10 @@ void MVKExample::setRefreshPeriod(double refreshPeriod) { // SRS - set Vul
_vulkanExample->refreshPeriod = refreshPeriod; _vulkanExample->refreshPeriod = refreshPeriod;
} }
void MVKExample::keyPressed(uint32_t keyChar) { // SRS - handle iOS virtual screen keyboard presses void MVKExample::keyPressed(uint32_t keyChar) { // SRS - handle keyboard key presses only (e.g. Pause, Space, etc)
switch (keyChar) switch (keyChar)
{ {
case 'p': case KEY_P:
case 'P':
_vulkanExample->paused = !_vulkanExample->paused; _vulkanExample->paused = !_vulkanExample->paused;
break; break;
default: default:
@ -34,34 +33,25 @@ void MVKExample::keyPressed(uint32_t keyChar) { // SRS - handle iOS virtual
} }
} }
void MVKExample::keyDown(uint32_t keyChar) { // SRS - handle physical keyboard key down/up actions void MVKExample::keyDown(uint32_t keyChar) { // SRS - handle physical keyboard key down/up actions and presses
switch (keyChar) switch (keyChar)
{ {
case 'p': case KEY_W:
case 'P': case KEY_Z: // for French AZERTY keyboards
_vulkanExample->paused = !_vulkanExample->paused;
break;
case 'w':
case 'W':
case 'z': // for French AZERTY keyboards
case 'Z':
_vulkanExample->camera.keys.up = true; _vulkanExample->camera.keys.up = true;
break; break;
case 's': case KEY_S:
case 'S':
_vulkanExample->camera.keys.down = true; _vulkanExample->camera.keys.down = true;
break; break;
case 'a': case KEY_A:
case 'A': case KEY_Q: // for French AZERTY keyboards
case 'q': // for French AZERTY keyboards
case 'Q':
_vulkanExample->camera.keys.left = true; _vulkanExample->camera.keys.left = true;
break; break;
case 'd': case KEY_D:
case 'D':
_vulkanExample->camera.keys.right = true; _vulkanExample->camera.keys.right = true;
break; break;
default: default:
MVKExample::keyPressed(keyChar);
break; break;
} }
} }
@ -69,24 +59,18 @@ void MVKExample::keyDown(uint32_t keyChar) { // SRS - handle physical keyboa
void MVKExample::keyUp(uint32_t keyChar) { void MVKExample::keyUp(uint32_t keyChar) {
switch (keyChar) switch (keyChar)
{ {
case 'w': case KEY_W:
case 'W': case KEY_Z: // for French AZERTY keyboards
case 'z': // for French AZERTY keyboards
case 'Z':
_vulkanExample->camera.keys.up = false; _vulkanExample->camera.keys.up = false;
break; break;
case 's': case KEY_S:
case 'S':
_vulkanExample->camera.keys.down = false; _vulkanExample->camera.keys.down = false;
break; break;
case 'a': case KEY_A:
case 'A': case KEY_Q: // for French AZERTY keyboards
case 'q': // for French AZERTY keyboards
case 'Q':
_vulkanExample->camera.keys.left = false; _vulkanExample->camera.keys.left = false;
break; break;
case 'd': case KEY_D:
case 'D':
_vulkanExample->camera.keys.right = false; _vulkanExample->camera.keys.right = false;
break; break;
default: default:

View file

@ -97,13 +97,13 @@ const std::string getAssetPath() {
// A key on the keyboard has been pressed. // A key on the keyboard has been pressed.
-(void) insertText: (NSString*) text { -(void) insertText: (NSString*) text {
unichar keychar = (text.length > 0) ? [text characterAtIndex: 0] : 0; unichar keychar = (text.length > 0) ? [text.lowercaseString characterAtIndex: 0] : 0;
_mvkExample->keyPressed(keychar); _mvkExample->keyPressed(keychar);
} }
// The delete backward key has been pressed. // The delete backward key has been pressed.
-(void) deleteBackward { -(void) deleteBackward {
_mvkExample->keyPressed(0x7F); _mvkExample->keyPressed(KEY_DELETE);
} }

View file

@ -91,13 +91,13 @@ MVKExample* _mvkExample;
// SRS - Handle keyboard events // SRS - Handle keyboard events
-(void) keyDown:(NSEvent*) theEvent { -(void) keyDown:(NSEvent*) theEvent {
NSString *text = [theEvent charactersIgnoringModifiers]; NSString *text = [theEvent charactersIgnoringModifiers];
unichar keychar = (text.length > 0) ? [text characterAtIndex: 0] : 0; unichar keychar = (text.length > 0) ? [text.lowercaseString characterAtIndex: 0] : 0;
_mvkExample->keyDown(keychar); _mvkExample->keyDown(keychar);
} }
-(void) keyUp:(NSEvent*) theEvent { -(void) keyUp:(NSEvent*) theEvent {
NSString *text = [theEvent charactersIgnoringModifiers]; NSString *text = [theEvent charactersIgnoringModifiers];
unichar keychar = (text.length > 0) ? [text characterAtIndex: 0] : 0; unichar keychar = (text.length > 0) ? [text.lowercaseString characterAtIndex: 0] : 0;
_mvkExample->keyUp(keychar); _mvkExample->keyUp(keychar);
} }