SendKeys.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef __SENDKEYS_04192004__INC__
  2. #define __SENDKEYS_04192004__INC__
  3. #include <windows.h>
  4. #include <tchar.h>
  5. // Please see SendKeys.cpp for copyright and usage issues.
  6. class CSendKeys
  7. {
  8. private:
  9. bool m_bWait, m_bUsingParens, m_bShiftDown, m_bLShiftDown, m_bRShiftDown, m_bAltDown, m_bControlDown, m_bLControlDown, m_bRControlDown, m_bWinDown;
  10. DWORD m_nDelayAlways, m_nDelayNow, m_keyDownDelay;
  11. static BOOL CALLBACK enumwindowsProc(HWND hwnd, LPARAM lParam);
  12. void CarryDelay();
  13. typedef BYTE KEYBOARDSTATE_t[256];
  14. struct enumwindow_t
  15. {
  16. LPTSTR str;
  17. HWND hwnd;
  18. };
  19. struct key_desc_t
  20. {
  21. LPCTSTR keyName;
  22. BYTE VKey;
  23. bool normalkey; // a normal character or a VKEY ?
  24. };
  25. enum
  26. {
  27. MaxSendKeysRecs = 71,
  28. MaxExtendedVKeys = 12
  29. };
  30. /*
  31. Reference: VkKeyScan() / MSDN
  32. Bit Meaning
  33. --- --------
  34. 1 Either SHIFT key is pressed.
  35. 2 Either CTRL key is pressed.
  36. 4 Either ALT key is pressed.
  37. 8 The Hankaku key is pressed
  38. 16 Reserved (defined by the keyboard layout driver).
  39. 32 Reserved (defined by the keyboard layout driver).
  40. */
  41. static const WORD VKKEYSCANSHIFTON;
  42. static const WORD VKKEYSCANCTRLON;
  43. static const WORD VKKEYSCANALTON;
  44. static const WORD INVALIDKEY;
  45. static key_desc_t KeyNames[MaxSendKeysRecs];
  46. static const BYTE ExtendedVKeys[MaxExtendedVKeys];
  47. static bool BitSet(BYTE BitTable, UINT BitMask);
  48. void PopUpShiftKeys();
  49. static bool IsVkExtended(BYTE VKey);
  50. void SendKeyUp(BYTE VKey);
  51. void SendKeyDown(BYTE VKey, WORD NumTimes, bool GenUpMsg, bool bDelay = false);
  52. void SendKey(WORD MKey, WORD NumTimes, bool GenDownMsg);
  53. static WORD StringToVKey(LPCTSTR KeyString, int &idx);
  54. void KeyboardEvent(BYTE VKey, BYTE ScanCode, LONG Flags);
  55. public:
  56. void AllKeysUp();
  57. bool SendKeys(LPCTSTR KeysString, bool Wait = false);
  58. static bool AppActivate(HWND wnd);
  59. static bool AppActivate(LPCTSTR WindowTitle, LPCTSTR WindowClass = 0);
  60. void SetDelay(const DWORD delay) { m_nDelayAlways = delay; }
  61. void SetKeyDownDelay(const DWORD delay) { m_keyDownDelay = delay; }
  62. static CString VkString(BYTE VKey);
  63. CSendKeys();
  64. };
  65. #endif