]> git.saurik.com Git - wxWidgets.git/commitdiff
wxGetKeyState as per feature request :).
authorRyan Norton <wxprojects@comcast.net>
Sun, 14 Dec 2003 23:35:21 +0000 (23:35 +0000)
committerRyan Norton <wxprojects@comcast.net>
Sun, 14 Dec 2003 23:35:21 +0000 (23:35 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@24854 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/utils.h
src/mac/app.cpp
src/mac/carbon/app.cpp
src/msw/window.cpp
src/unix/utilsx11.cpp

index 7dd95c8923cced54e063ebdf3257478ea348475f..fa670285168444777c4f45a9efa18e5f4bfc903b 100644 (file)
@@ -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();
 
index 0c8413941f4efb50b6e490385bd01b6b8b1a898e..36165f1246977a79afcbe8280133a8b93adeb95f 100644 (file)
@@ -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 )
 {
index 0c8413941f4efb50b6e490385bd01b6b8b1a898e..36165f1246977a79afcbe8280133a8b93adeb95f 100644 (file)
@@ -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 )
 {
index d3bc7241d7e51c063bbface7a5582ae283a647a6..081a98761b990e45d863f055b98d794235c34ca4 100644 (file)
@@ -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();
index 85256f048ed26999efa89d5178ce6c8e245a3262..2a6eaf0deb17ac9ff02ba6c9570616f403f04d4e 100644 (file)
@@ -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