From 6ed892f3f2be3ec8f96583772757aecc00bbee30 Mon Sep 17 00:00:00 2001 From: Ryan Norton Date: Sun, 14 Dec 2003 23:35:21 +0000 Subject: [PATCH] wxGetKeyState as per feature request :). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@24854 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/utils.h | 5 +++++ src/mac/app.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/mac/carbon/app.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/msw/window.cpp | 12 ++++++++++++ src/unix/utilsx11.cpp | 23 +++++++++++++++++++++++ 5 files changed, 112 insertions(+) diff --git a/include/wx/utils.h b/include/wx/utils.h index 7dd95c8923..fa67028516 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -80,6 +80,11 @@ WXDLLIMPEXP_BASE wxChar* copystring(const wxChar *s); // Sound the bell WXDLLIMPEXP_BASE void wxBell(); +#if defined(__WXGTK__) || defined(__WXMSW__) || defined(__WXMAC__) || defined(__X__) + // Get the state of a key (true if pressed, false if not) + WXDLLIMPEXP_BASE bool wxGetKeyState(wxKeyCode key); +#endif + // Get OS description as a user-readable string WXDLLIMPEXP_BASE wxString wxGetOsDescription(); diff --git a/src/mac/app.cpp b/src/mac/app.cpp index 0c8413941f..36165f1246 100644 --- a/src/mac/app.cpp +++ b/src/mac/app.cpp @@ -1757,6 +1757,42 @@ long wxMacTranslateKey(unsigned char key, unsigned char code) return retval; } +int wxKeyCodeToMacModifier(wxKeyCode key) +{ + switch (key) + { + case WXK_START: + return cmdKey; + + case WXK_SHIFT: + return shiftKey; + + case WXK_CAPITAL: + return alphaLock; + + case WXK_OPTION: + return optionKey; + + case WXK_CONTROL: + return controlKey; + + default: + return 0; + } +} + +bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below +{ +//if OS X > 10.2 (i.e. 10.2.x) +//a known apple bug prevents the system from determining led +//states with GetKeys... can only determine caps lock led + return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key)); +//else +// KeyMapByteArray keymap; +// GetKeys((BigEndianLong*)keymap); +// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey)); +} + #if !TARGET_CARBON void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr ) { diff --git a/src/mac/carbon/app.cpp b/src/mac/carbon/app.cpp index 0c8413941f..36165f1246 100644 --- a/src/mac/carbon/app.cpp +++ b/src/mac/carbon/app.cpp @@ -1757,6 +1757,42 @@ long wxMacTranslateKey(unsigned char key, unsigned char code) return retval; } +int wxKeyCodeToMacModifier(wxKeyCode key) +{ + switch (key) + { + case WXK_START: + return cmdKey; + + case WXK_SHIFT: + return shiftKey; + + case WXK_CAPITAL: + return alphaLock; + + case WXK_OPTION: + return optionKey; + + case WXK_CONTROL: + return controlKey; + + default: + return 0; + } +} + +bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below +{ +//if OS X > 10.2 (i.e. 10.2.x) +//a known apple bug prevents the system from determining led +//states with GetKeys... can only determine caps lock led + return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key)); +//else +// KeyMapByteArray keymap; +// GetKeys((BigEndianLong*)keymap); +// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey)); +} + #if !TARGET_CARBON void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr ) { diff --git a/src/msw/window.cpp b/src/msw/window.cpp index d3bc7241d7..081a98761b 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -5057,6 +5057,18 @@ int wxCharCodeWXToMSW(int id, bool *isVirtual) return keySym; } +bool wxGetKeyState(wxKeyCode key) +{ + bool bVirtual; + int vkey = wxCharCodeWXToMSW(key, &bVirtual); + + //there aren't WXK_ macros for non-virtual key codes + if (bVirtual == false) + return false; + + return GetKeyState(vkey) < 0; +} + wxWindow *wxGetActiveWindow() { HWND hWnd = GetActiveWindow(); diff --git a/src/unix/utilsx11.cpp b/src/unix/utilsx11.cpp index 85256f048e..2a6eaf0deb 100644 --- a/src/unix/utilsx11.cpp +++ b/src/unix/utilsx11.cpp @@ -77,6 +77,7 @@ public: XFlush(m_display); XSetErrorHandler(m_old); } + private: Display *m_display; int (*m_old)(Display*, XErrorEvent *); @@ -505,5 +506,27 @@ void wxSetFullScreenStateX11(WXDisplay* display, WXWindow rootWindow, } } +bool wxGetKeyState(wxKeyCode key) +{ + Display *pDisplay = wxApp::GetDisplay(); + int iKey = wxCharCodeWXToX(key); + int iKeyMask = 0; + Window wDummy1, wDummy2; + int iDummy3, iDummy4, iDummy5, iDummy6; + unsigned int iMask; + XModifierKeymap* map = XGetModifierMapping(pDisplay); + KeyCode keyCode = XKeysymToKeycode(pDisplay,iKey); + if(keyCode == NoSymbol) return false; + for(int i = 0; i < 8; ++i) { + if( map->modifiermap[map->max_keypermod * i] == keyCode) { + iKeyMask = 1 << i; + } + } + XQueryPointer(pDisplay, DefaultRootWindow(pDisplay), &wDummy1, &wDummy2, + &iDummy3, &iDummy4, &iDummy5, &iDummy6, &iMask ); + XFreeModifiermap(map); + return (iMask & iKeyMask) != 0; +} + #endif -- 2.45.2