From 7d30eaf13a5097684dffa66cb05a0cc1ed612804 Mon Sep 17 00:00:00 2001 From: Chris Pavlina Date: Fri, 15 Jan 2016 20:58:31 -0500 Subject: [PATCH] Refactor keycode lookup --- common/widgets/widget_hotkey_list.cpp | 76 ++++++++++++++++----------- include/widgets/widget_hotkey_list.h | 6 +++ 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/common/widgets/widget_hotkey_list.cpp b/common/widgets/widget_hotkey_list.cpp index 8c2aafea14..2d3109af87 100644 --- a/common/widgets/widget_hotkey_list.cpp +++ b/common/widgets/widget_hotkey_list.cpp @@ -230,40 +230,15 @@ void WIDGET_HOTKEY_LIST::OnActivated( wxTreeListEvent& aEvent ) if( hkdata ) { - long key = key_event.GetKeyCode(); + long key = MapKeypressToKeycode( key_event ); - switch( key ) + if( key == 0 ) { - case WXK_ESCAPE: + // key: Escape UnselectAll(); - break; - - default: - if( key >= 'a' && key <= 'z' ) // convert to uppercase - key = key + ('A' - 'a'); - - // Remap Ctrl A (=1+GR_KB_CTRL) to Ctrl Z(=26+GR_KB_CTRL) - // to GR_KB_CTRL+'A' .. GR_KB_CTRL+'Z' - if( key_event.ControlDown() && key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z ) - key += 'A' - 1; - - /* Disallow shift for keys that have two keycodes on them (e.g. number and - * punctuation keys) leaving only the "letter keys" of A-Z. - * Then, you can have, e.g. Ctrl-5 and Ctrl-% (GB layout) - * and Ctrl-( and Ctrl-5 (FR layout). - * Otherwise, you'd have to have to say Ctrl-Shift-5 on a FR layout - */ - bool keyIsLetter = key >= 'A' && key <= 'Z'; - - if( key_event.ShiftDown() && ( keyIsLetter || key > 256 ) ) - key |= GR_KB_SHIFT; - - if( key_event.ControlDown() ) - key |= GR_KB_CTRL; - - if( key_event.AltDown() ) - key |= GR_KB_ALT; - + } + else + { // See if this key code is handled in hotkeys names list bool exists; KeyNameFromKeyCode( key, &exists ); @@ -486,3 +461,42 @@ bool WIDGET_HOTKEY_LIST::TransferDataFromControl() return true; } + +long WIDGET_HOTKEY_LIST::MapKeypressToKeycode( const wxKeyEvent& aEvent ) +{ + long key = aEvent.GetKeyCode(); + + if( key == WXK_ESCAPE ) + { + return 0; + } + else + { + if( key >= 'a' && key <= 'z' ) // convert to uppercase + key = key + ('A' - 'a'); + + // Remap Ctrl A (=1+GR_KB_CTRL) to Ctrl Z(=26+GR_KB_CTRL) + // to GR_KB_CTRL+'A' .. GR_KB_CTRL+'Z' + if( aEvent.ControlDown() && key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z ) + key += 'A' - 1; + + /* Disallow shift for keys that have two keycodes on them (e.g. number and + * punctuation keys) leaving only the "letter keys" of A-Z. + * Then, you can have, e.g. Ctrl-5 and Ctrl-% (GB layout) + * and Ctrl-( and Ctrl-5 (FR layout). + * Otherwise, you'd have to have to say Ctrl-Shift-5 on a FR layout + */ + bool keyIsLetter = key >= 'A' && key <= 'Z'; + + if( aEvent.ShiftDown() && ( keyIsLetter || key > 256 ) ) + key |= GR_KB_SHIFT; + + if( aEvent.ControlDown() ) + key |= GR_KB_CTRL; + + if( aEvent.AltDown() ) + key |= GR_KB_ALT; + + return key; + } +} diff --git a/include/widgets/widget_hotkey_list.h b/include/widgets/widget_hotkey_list.h index 29939b0fcf..d851111786 100644 --- a/include/widgets/widget_hotkey_list.h +++ b/include/widgets/widget_hotkey_list.h @@ -164,6 +164,12 @@ public: * @return true iff the operation was successful */ bool TransferDataFromControl(); + + /** + * Static method MapKeypressToKeycode + * Map a keypress event to the correct key code for use as a hotkey. + */ + static long MapKeypressToKeycode( const wxKeyEvent& aEvent ); }; #endif // __widget_hotkey_list__