From e4a4578b407054fdbcdda244c416016dfc37c5d1 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 9 Apr 2014 17:33:22 +0200 Subject: [PATCH] Key events are handled by wxEVT_CHAR instead of wxEVT_KEY_[UP|DOWN]. Fixed issue of chars that require modifiers (e.g. ? is Shift+/ on US keyboard layout). --- common/draw_panel_gal.cpp | 3 +-- common/tool/action_manager.cpp | 16 +++++++++++++-- common/tool/tool_dispatcher.cpp | 21 ++++++++++--------- common/tool/tool_event.cpp | 3 +-- common/tool/tool_manager.cpp | 2 +- include/tool/tool_event.h | 36 ++++++++++++++------------------- pcbnew/router/router_tool.cpp | 14 ++++++------- pcbnew/tools/common_actions.cpp | 10 ++++----- pcbnew/tools/drawing_tool.cpp | 8 ++++---- 9 files changed, 60 insertions(+), 53 deletions(-) diff --git a/common/draw_panel_gal.cpp b/common/draw_panel_gal.cpp index a8c1dc0f67..0ec6a1130b 100644 --- a/common/draw_panel_gal.cpp +++ b/common/draw_panel_gal.cpp @@ -83,8 +83,7 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin Connect( wxEVT_MIDDLE_DCLICK, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); Connect( wxEVT_MOUSEWHEEL, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); Connect( wxEVT_CHAR_HOOK, wxEventHandler( EDA_DRAW_PANEL_GAL::skipEvent ) ); - Connect( wxEVT_KEY_UP, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); - Connect( wxEVT_KEY_DOWN, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); + Connect( wxEVT_CHAR, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this ); Connect( KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this ); diff --git a/common/tool/action_manager.cpp b/common/tool/action_manager.cpp index 24ee6ca818..51c85aa273 100644 --- a/common/tool/action_manager.cpp +++ b/common/tool/action_manager.cpp @@ -115,10 +115,22 @@ void ACTION_MANAGER::RunAction( const TOOL_ACTION* aAction ) const bool ACTION_MANAGER::RunHotKey( int aHotKey ) const { - HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( aHotKey ); + int key = std::toupper( aHotKey & ~MD_MODIFIER_MASK ); + int mod = aHotKey & MD_MODIFIER_MASK; + HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod ); + + // If no luck, try without modifier, to handle keys that require a modifier + // e.g. to get ? you need to press Shift+/ without US keyboard layout + // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a + // different combination if( it == m_actionHotKeys.end() ) - return false; // no appropriate action found for the hotkey + { + it = m_actionHotKeys.find( key ); + + if( it == m_actionHotKeys.end() ) + return false; // no appropriate action found for the hotkey + } const std::list& actions = it->second; diff --git a/common/tool/tool_dispatcher.cpp b/common/tool/tool_dispatcher.cpp index f89c7ee03f..a387d6581a 100644 --- a/common/tool/tool_dispatcher.cpp +++ b/common/tool/tool_dispatcher.cpp @@ -243,23 +243,26 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent ) } // Keyboard handling - else if( type == wxEVT_KEY_UP || type == wxEVT_KEY_DOWN ) + else if( type == wxEVT_CHAR ) { wxKeyEvent* ke = static_cast( &aEvent ); int key = ke->GetKeyCode(); int mods = decodeModifiers( ke ); - if( type == wxEVT_KEY_UP ) + if( mods & MD_CTRL ) { - evt = TOOL_EVENT( TC_KEYBOARD, TA_KEY_UP, key | mods ); + // wxWidgets have a quirk related to Ctrl+letter hot keys handled by CHAR_EVT + // http://docs.wxwidgets.org/trunk/classwx_key_event.html: + // "char events for ASCII letters in this case carry codes corresponding to the ASCII + // value of Ctrl-Latter, i.e. 1 for Ctrl-A, 2 for Ctrl-B and so on until 26 for Ctrl-Z." + if( key >= WXK_CONTROL_A && key <= WXK_CONTROL_Z ) + key += 'A' - 1; } + + if( key == WXK_ESCAPE ) // ESC is the special key for cancelling tools + evt = TOOL_EVENT( TC_COMMAND, TA_CANCEL_TOOL ); else - { - if( key == WXK_ESCAPE ) // ESC is the special key for cancelling tools - evt = TOOL_EVENT( TC_COMMAND, TA_CANCEL_TOOL ); - else - evt = TOOL_EVENT( TC_KEYBOARD, TA_KEY_DOWN, key | mods ); - } + evt = TOOL_EVENT( TC_KEYBOARD, TA_KEY_PRESSED, key | mods ); } if( evt ) diff --git a/common/tool/tool_event.cpp b/common/tool/tool_event.cpp index 07460f3571..50739ba419 100644 --- a/common/tool/tool_event.cpp +++ b/common/tool/tool_event.cpp @@ -81,8 +81,7 @@ const std::string TOOL_EVENT::Format() const { TA_MOUSE_DRAG, "drag" }, { TA_MOUSE_MOTION, "motion" }, { TA_MOUSE_WHEEL, "wheel" }, - { TA_KEY_UP, "key-up" }, - { TA_KEY_DOWN, "key-down" }, + { TA_KEY_PRESSED, "key-pressed" }, { TA_VIEW_REFRESH, "view-refresh" }, { TA_VIEW_ZOOM, "view-zoom" }, { TA_VIEW_PAN, "view-pan" }, diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 8c05ca5282..e83b8fc6d2 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -417,7 +417,7 @@ void TOOL_MANAGER::dispatchInternal( TOOL_EVENT& aEvent ) bool TOOL_MANAGER::dispatchStandardEvents( TOOL_EVENT& aEvent ) { - if( aEvent.Action() == TA_KEY_UP ) + if( aEvent.Action() == TA_KEY_PRESSED ) { // Check if there is a hotkey associated if( m_actionMgr->RunHotKey( aEvent.Modifier() | aEvent.KeyCode() ) ) diff --git a/include/tool/tool_event.h b/include/tool/tool_event.h index 4571b5a9f8..bdfe8a8a10 100644 --- a/include/tool/tool_event.h +++ b/include/tool/tool_event.h @@ -64,36 +64,35 @@ enum TOOL_ACTIONS TA_MOUSE_WHEEL = 0x0040, TA_MOUSE = 0x007f, - TA_KEY_UP = 0x0080, - TA_KEY_DOWN = 0x0100, - TA_KEYBOARD = TA_KEY_UP | TA_KEY_DOWN, + TA_KEY_PRESSED = 0x0080, + TA_KEYBOARD = TA_KEY_PRESSED, // View related events - TA_VIEW_REFRESH = 0x0200, - TA_VIEW_ZOOM = 0x0400, - TA_VIEW_PAN = 0x0800, - TA_VIEW_DIRTY = 0x1000, - TA_VIEW = 0x1e00, + TA_VIEW_REFRESH = 0x0100, + TA_VIEW_ZOOM = 0x0200, + TA_VIEW_PAN = 0x0400, + TA_VIEW_DIRTY = 0x0800, + TA_VIEW = 0x0f00, - TA_CHANGE_LAYER = 0x2000, + TA_CHANGE_LAYER = 0x1000, // Tool cancel event. Issued automagically when the user hits escape or selects End Tool from // the context menu. - TA_CANCEL_TOOL = 0x4000, + TA_CANCEL_TOOL = 0x2000, // Context menu update. Issued whenever context menu is open and the user hovers the mouse // over one of choices. Used in dynamic highligting in disambiguation menu - TA_CONTEXT_MENU_UPDATE = 0x8000, + TA_CONTEXT_MENU_UPDATE = 0x4000, // Context menu choice. Sent if the user picked something from the context menu or // closed it without selecting anything. - TA_CONTEXT_MENU_CHOICE = 0x10000, + TA_CONTEXT_MENU_CHOICE = 0x8000, // This event is sent *before* undo/redo command is performed. - TA_UNDO_REDO = 0x20000, + TA_UNDO_REDO = 0x10000, // Tool action (allows to control tools) - TA_ACTION = 0x40000, + TA_ACTION = 0x20000, TA_ANY = 0xffffffff }; @@ -277,14 +276,9 @@ public: return m_keyCode; } - bool IsKeyUp() const + bool IsKeyPressed() const { - return m_actions == TA_KEY_UP; - } - - bool IsKeyDown() const - { - return m_actions == TA_KEY_DOWN; + return m_actions == TA_KEY_PRESSED; } void SetMouseDragOrigin( const VECTOR2D& aP ) diff --git a/pcbnew/router/router_tool.cpp b/pcbnew/router/router_tool.cpp index dd24327678..8fe733fb8c 100644 --- a/pcbnew/router/router_tool.cpp +++ b/pcbnew/router/router_tool.cpp @@ -45,11 +45,11 @@ using namespace KIGFX; using boost::optional; -static TOOL_ACTION ACT_AutoEndRoute( "pcbnew.InteractiveRouter.AutoEndRoute", AS_CONTEXT, 'G' ); -static TOOL_ACTION ACT_PlaceVia( "pcbnew.InteractiveRouter.PlaceVia", AS_CONTEXT, 'V' ); -static TOOL_ACTION ACT_OpenRouteOptions( "pcbnew.InteractiveRouter.OpenRouterOptions", AS_CONTEXT, 'T' ); -static TOOL_ACTION ACT_SwitchPosture( "pcbnew.InteractiveRouter.SwitchPosture", AS_CONTEXT, '/' ); -static TOOL_ACTION ACT_EndTrack( "pcbnew.InteractiveRouter.EndTrack", AS_CONTEXT, WXK_END ); +//static TOOL_ACTION ACT_AutoEndRoute( "pcbnew.InteractiveRouter.AutoEndRoute", AS_CONTEXT, 'G' ); +//static TOOL_ACTION ACT_PlaceVia( "pcbnew.InteractiveRouter.PlaceVia", AS_CONTEXT, 'V' ); +//static TOOL_ACTION ACT_OpenRouteOptions( "pcbnew.InteractiveRouter.OpenRouterOptions", AS_CONTEXT, 'T' ); +//static TOOL_ACTION ACT_SwitchPosture( "pcbnew.InteractiveRouter.SwitchPosture", AS_CONTEXT, '/' ); +//static TOOL_ACTION ACT_EndTrack( "pcbnew.InteractiveRouter.EndTrack", AS_CONTEXT, WXK_END ); ROUTER_TOOL::ROUTER_TOOL() : TOOL_INTERACTIVE( "pcbnew.InteractiveRouter" ) @@ -367,9 +367,9 @@ void ROUTER_TOOL::startRouting() m_router->Move( m_endSnapPoint, m_endItem ); } - else if( evt->IsKeyUp() ) + else if( evt->IsKeyPressed() ) { - switch( evt->KeyCode() ) + switch( std::toupper( evt->KeyCode() ) ) { case 'V': { diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index 90a235e9ac..3ff65af56b 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -148,11 +148,11 @@ TOOL_ACTION COMMON_ACTIONS::highContrastMode( "pcbnew.highContrastMode", "", "" ); TOOL_ACTION COMMON_ACTIONS::highContrastInc( "pcbnew.highContrastInc", - AS_GLOBAL, MD_SHIFT + '.', // shift+. == > + AS_GLOBAL, '>', "", "" ); TOOL_ACTION COMMON_ACTIONS::highContrastDec( "pcbnew.highContrastDec", - AS_GLOBAL, MD_SHIFT + 60, // shift+, == < + AS_GLOBAL, '<', "", "" ); @@ -198,11 +198,11 @@ TOOL_ACTION COMMON_ACTIONS::layerPrev( "pcbnew.layerPrev", "", "" ); TOOL_ACTION COMMON_ACTIONS::layerAlphaInc( "pcbnew.layerAlphaInc", - AS_GLOBAL, MD_SHIFT + ']', // shift+] == } + AS_GLOBAL, '}', "", "" ); TOOL_ACTION COMMON_ACTIONS::layerAlphaDec( "pcbnew.layerAlphaDec", - AS_GLOBAL, MD_SHIFT + '[', // shift+[ == { + AS_GLOBAL, '{', "", "" ); @@ -252,7 +252,7 @@ TOOL_ACTION COMMON_ACTIONS::switchUnits( "pcbnew.switchUnits", "", "" ); TOOL_ACTION COMMON_ACTIONS::showHelp( "pcbnew.showHelp", - AS_GLOBAL, MD_SHIFT + '/', // shift+/ == ? + AS_GLOBAL, '?', "", "" ); diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 1d85ec6906..34af899d35 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -135,7 +135,7 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) break; } - else if( evt->IsKeyUp() && step != SET_ORIGIN ) + else if( evt->IsKeyPressed() && step != SET_ORIGIN ) { int width = arc->GetWidth(); @@ -427,7 +427,7 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent ) break; } - else if( evt->IsKeyUp() && step != SET_ORIGIN ) + else if( evt->IsKeyPressed() && step != SET_ORIGIN ) { width = dimension->GetWidth(); @@ -603,7 +603,7 @@ int DRAWING_TOOL::PlaceTarget( TOOL_EVENT& aEvent ) if( evt->IsCancel() ) break; - else if( evt->IsKeyUp() ) + else if( evt->IsKeyPressed() ) { int width = target->GetWidth(); @@ -822,7 +822,7 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous ) m_controls->SetAutoPan( false ); } - else if( graphic && evt->IsKeyUp() ) + else if( graphic && evt->IsKeyPressed() ) { int width = graphic->GetWidth();