Refactor keycode lookup

This commit is contained in:
Chris Pavlina 2016-01-15 20:58:31 -05:00 committed by Chris Pavlina
parent 87cb6c4429
commit 7d30eaf13a
2 changed files with 51 additions and 31 deletions

View File

@ -230,40 +230,15 @@ void WIDGET_HOTKEY_LIST::OnActivated( wxTreeListEvent& aEvent )
if( hkdata ) if( hkdata )
{ {
long key = key_event.GetKeyCode(); long key = MapKeypressToKeycode( key_event );
switch( key ) if( key == 0 )
{ {
case WXK_ESCAPE: // key: Escape
UnselectAll(); UnselectAll();
break; }
else
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;
// See if this key code is handled in hotkeys names list // See if this key code is handled in hotkeys names list
bool exists; bool exists;
KeyNameFromKeyCode( key, &exists ); KeyNameFromKeyCode( key, &exists );
@ -486,3 +461,42 @@ bool WIDGET_HOTKEY_LIST::TransferDataFromControl()
return true; 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;
}
}

View File

@ -164,6 +164,12 @@ public:
* @return true iff the operation was successful * @return true iff the operation was successful
*/ */
bool TransferDataFromControl(); 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__ #endif // __widget_hotkey_list__