home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / keyboard_mouse.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-02-05  |  16.1 KB  |  342 lines

  1. /*
  2. AutoHotkey
  3.  
  4. Copyright 2003-2007 Chris Mallett (support@autohotkey.com)
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15. */
  16.  
  17. #ifndef keyboard_h
  18. #define keyboard_h
  19.  
  20. #include "defines.h"
  21.  
  22. // The max number of keystrokes to Send prior to taking a break to pump messages:
  23. #define MAX_LUMP_KEYS 50
  24.  
  25. // Logging keys to a file is disabled in the main version in an effort to prevent
  26. // AutoHotkey from being branded as a key logger or trojan by various security firms and
  27. // security software. Uncomment this line to re-enabled logging of keys to a file:
  28. // #define ENABLE_KEY_HISTORY_FILE
  29.  
  30. // Maybe define more of these later, perhaps with ifndef (since they should be in the normal header, and probably
  31. // will be eventually):
  32. // ALREADY DEFINED: #define VK_HELP 0x2F
  33. // In case a compiler with a non-updated header file is used:
  34. #ifndef VK_BROWSER_BACK
  35.     #define VK_BROWSER_BACK        0xA6
  36.     #define VK_BROWSER_FORWARD     0xA7
  37.     #define VK_BROWSER_REFRESH     0xA8
  38.     #define VK_BROWSER_STOP        0xA9
  39.     #define VK_BROWSER_SEARCH      0xAA
  40.     #define VK_BROWSER_FAVORITES   0xAB
  41.     #define VK_BROWSER_HOME        0xAC
  42.     #define VK_VOLUME_MUTE         0xAD
  43.     #define VK_VOLUME_DOWN         0xAE
  44.     #define VK_VOLUME_UP           0xAF
  45.     #define VK_MEDIA_NEXT_TRACK    0xB0
  46.     #define VK_MEDIA_PREV_TRACK    0xB1
  47.     #define VK_MEDIA_STOP          0xB2
  48.     #define VK_MEDIA_PLAY_PAUSE    0xB3
  49.     #define VK_LAUNCH_MAIL         0xB4
  50.     #define VK_LAUNCH_MEDIA_SELECT 0xB5
  51.     #define VK_LAUNCH_APP1         0xB6
  52.     #define VK_LAUNCH_APP2         0xB7
  53. #endif
  54.  
  55. // Create some "fake" virtual keys to simplify sections of the code.
  56. // According to winuser.h, the following ranges (among others)
  57. // are considered "unassigned" rather than "reserved", so should be
  58. // fairly safe to use for the foreseeable future.  0xFF should probably
  59. // be avoided since it's sometimes used as a failure indictor by API
  60. // calls.  And 0x00 should definitely be avoided because it is used
  61. // to indicate failure by many functions that deal with virtual keys.
  62. // 0x88 - 0x8F : unassigned
  63. // 0x97 - 0x9F : unassigned (this range seems less likely to be used)
  64. #define VK_NEW_MOUSE_FIRST 0x9C
  65. #define VK_LBUTTON_LOGICAL 0x9C // v1.0.43: Added to support swapping of left/right mouse buttons in Control Panel.
  66. #define VK_RBUTTON_LOGICAL 0x9D //
  67. #define VK_WHEEL_DOWN      0x9E
  68. #define VK_WHEEL_UP        0x9F
  69. #define VK_NEW_MOUSE_LAST  0x9F
  70.  
  71. // These are the only keys for which another key with the same VK exists.  Therefore, use scan code for these.
  72. // If use VK for some of these (due to them being more likely to be used as hotkeys, thus minimizing the
  73. // use of the keyboard hook), be sure to use SC for its counterpart.
  74. // Always use the compressed version of scancode, i.e. 0x01 for the high-order byte rather than vs. 0xE0.
  75. #define SC_NUMPADENTER 0x11C
  76. #define SC_INSERT 0x152
  77. #define SC_DELETE 0x153
  78. #define SC_HOME 0x147
  79. #define SC_END 0x14F
  80. #define SC_UP 0x148
  81. #define SC_DOWN 0x150
  82. #define SC_LEFT 0x14B
  83. #define SC_RIGHT 0x14D
  84. #define SC_PGUP 0x149
  85. #define SC_PGDN 0x151
  86.  
  87. // These are the same scan codes as their counterpart except the extended flag is 0 rather than
  88. // 1 (0xE0 uncompressed):
  89. #define SC_ENTER 0x1C
  90. // In addition, the below dual-state numpad keys share the same scan code (but different vk's)
  91. // regardless of the state of numlock:
  92. #define SC_NUMPADDEL 0x53
  93. #define SC_NUMPADINS 0x52
  94. #define SC_NUMPADEND 0x4F
  95. #define SC_NUMPADHOME 0x47
  96. #define SC_NUMPADCLEAR 0x4C
  97. #define SC_NUMPADUP 0x48
  98. #define SC_NUMPADDOWN 0x50
  99. #define SC_NUMPADLEFT 0x4B
  100. #define SC_NUMPADRIGHT 0x4D
  101. #define SC_NUMPADPGUP 0x49
  102. #define SC_NUMPADPGDN 0x51
  103.  
  104. #define SC_NUMPADDOT SC_NUMPADDEL
  105. #define SC_NUMPAD0 SC_NUMPADINS
  106. #define SC_NUMPAD1 SC_NUMPADEND
  107. #define SC_NUMPAD2 SC_NUMPADDOWN
  108. #define SC_NUMPAD3 SC_NUMPADPGDN
  109. #define SC_NUMPAD4 SC_NUMPADLEFT
  110. #define SC_NUMPAD5 SC_NUMPADCLEAR
  111. #define SC_NUMPAD6 SC_NUMPADRIGHT
  112. #define SC_NUMPAD7 SC_NUMPADHOME
  113. #define SC_NUMPAD8 SC_NUMPADUP
  114. #define SC_NUMPAD9 SC_NUMPADPGUP
  115.  
  116. // These both have a unique vk and a unique sc (on most keyboards?), but they're listed here because
  117. // MapVirtualKey doesn't support them under Win9x (except maybe NumLock itself):
  118. #define SC_NUMLOCK 0x145
  119. #define SC_NUMPADDIV 0x135
  120. #define SC_NUMPADMULT 0x037
  121. #define SC_NUMPADSUB 0x04A
  122. #define SC_NUMPADADD 0x04E
  123.  
  124. // Note: A KeyboardProc() (hook) actually receives 0x36 for RSHIFT under both WinXP and Win98se, not 0x136.
  125. // All the below have been verified to be accurate under Win98se and XP (except rctrl and ralt in XP).
  126. #define SC_LCONTROL 0x01D
  127. #define SC_RCONTROL 0x11D
  128. #define SC_LSHIFT 0x02A
  129. #define SC_RSHIFT 0x136 // Must be extended, at least on WinXP, or there will be problems, e.g. SetModifierLRState().
  130. #define SC_LALT 0x038
  131. #define SC_RALT 0x138
  132. #define SC_LWIN 0x15B
  133. #define SC_RWIN 0x15C
  134.  
  135. // UPDATE for v1.0.39: Changed sc_type to USHORT vs. UINT to save memory in structs such as sc_hotkey.
  136. // This saves 60K of memory in one place, and possibly there are other large savings too.
  137. // The following older comment dates back to 2003/inception and I don't remember its exact intent,
  138. // but there is no current storage of mouse message constants in scan code variables:
  139. // OLD: Although only need 9 bits for compressed and 16 for uncompressed scan code, use a full 32 bits 
  140. // so that mouse messages (WPARAM) can be stored as scan codes.  Formerly USHORT (which is always 16-bit).
  141. typedef USHORT sc_type; // Scan code.
  142. typedef UCHAR vk_type;  // Virtual key.
  143. typedef UINT mod_type;  // Standard Windows modifier type for storing MOD_CONTROL, MOD_WIN, MOD_ALT, MOD_SHIFT.
  144.  
  145. // The maximum number of virtual keys and scan codes that can ever exist.
  146. // As of WinXP, these are absolute limits, except for scan codes for which there might conceivably
  147. // be more if any non-standard keyboard or keyboard drivers generate scan codes that don't start
  148. // with either 0x00 or 0xE0.  UPDATE: Decided to allow all possible scancodes, rather than just 512,
  149. // since a lookup array for the 16-bit scan code value will only consume 64K of RAM if the element
  150. // size is one char.  UPDATE: decided to go back to 512 scan codes, because WinAPI's KeyboardProc()
  151. // itself can only handle that many (a 9-bit value).  254 is the largest valid vk, according to the
  152. // WinAPI docs (I think 255 is value that is sometimes returned to indicate an invalid vk).  But
  153. // just in case something ever tries to access such arrays using the largest 8-bit value (255), add
  154. // 1 to make it 0xFF, thus ensuring array indexes will always be in-bounds if they are 8-bit values.
  155. #define VK_MAX 0xFF
  156. #define SC_MAX 0x1FF
  157.  
  158. typedef UCHAR modLR_type; // Only the left-right win/alt/ctrl/shift rather than their generic counterparts.
  159. #define MODLR_MAX 0xFF
  160. #define MODLR_COUNT 8
  161. #define MOD_LCONTROL 0x01
  162. #define MOD_RCONTROL 0x02
  163. #define MOD_LALT 0x04
  164. #define MOD_RALT 0x08
  165. #define MOD_LSHIFT 0x10
  166. #define MOD_RSHIFT 0x20
  167. #define MOD_LWIN 0x40
  168. #define MOD_RWIN 0x80
  169.  
  170.  
  171. struct CachedLayoutType
  172. {
  173.     HKL hkl;
  174.     ResultType has_altgr;
  175. };
  176.  
  177. struct key_to_vk_type // Map key names to virtual keys.
  178. {
  179.     char *key_name;
  180.     vk_type vk;
  181. };
  182.  
  183. struct key_to_sc_type // Map key names to scan codes.
  184. {
  185.     char *key_name;
  186.     sc_type sc;
  187. };
  188.  
  189. enum KeyStateTypes {KEYSTATE_LOGICAL, KEYSTATE_PHYSICAL, KEYSTATE_TOGGLE}; // For use with GetKeyJoyState(), etc.
  190. enum KeyEventTypes {KEYDOWN, KEYUP, KEYDOWNANDUP};
  191.  
  192. void SendKeys(char *aKeys, bool aSendRaw, SendModes aSendModeOrig, HWND aTargetWindow = NULL);
  193. void SendKey(vk_type aVK, sc_type aSC, modLR_type aModifiersLR, modLR_type aModifiersLRPersistent
  194.     , int aRepeatCount, KeyEventTypes aEventType, modLR_type aKeyAsModifiersLR, HWND aTargetWindow
  195.     , int aX = COORD_UNSPECIFIED, int aY = COORD_UNSPECIFIED, bool aMoveOffset = false);
  196. void SendKeySpecial(char aChar, int aRepeatCount);
  197. void SendASC(char *aAscii);
  198.  
  199. struct PlaybackEvent
  200. {
  201.     UINT message;
  202.     union
  203.     {
  204.         struct
  205.         {
  206.             sc_type sc; // Placed above vk for possibly better member stacking/alignment.
  207.             vk_type vk;
  208.         };
  209.         struct
  210.         {
  211.             // Screen coordinates, which can be negative.  SHORT vs. INT is used because the likelihood
  212.             // have having a virtual display surface wider or taller than 32,767 seems too remote to
  213.             // justify increasing the struct size, which would impact the stack space and dynamic memory
  214.             // used by every script every time it uses the playback method to send keystrokes or clicks.
  215.             // Note: WM_LBUTTONDOWN uses WORDs vs. SHORTs, but they're not really comparable because
  216.             // journal playback/record both use screen coordinates but WM_LBUTTONDOWN et. al. use client
  217.             // coordinates.
  218.             SHORT x;
  219.             SHORT y;
  220.         };
  221.         DWORD time_to_wait; // This member is present only when message==0; otherwise, a struct is present.
  222.     };
  223. };
  224. LRESULT CALLBACK PlaybackProc(int aCode, WPARAM wParam, LPARAM lParam);
  225. //#define JOURNAL_RECORD_MODE  // Uncomment this line to debug/analyze via a crude journal record feature in place of SendPlay.
  226. #ifdef JOURNAL_RECORD_MODE
  227.     LRESULT CALLBACK RecordProc(int aCode, WPARAM wParam, LPARAM lParam);
  228. #endif
  229.  
  230.  
  231. // Below uses a pseudo-random value.  It's best that this be constant so that if multiple instances
  232. // of the app are running, they will all ignore each other's keyboard & mouse events.  Also, a value
  233. // close to UINT_MAX might be a little better since it's might be less likely to be used as a pointer
  234. // value by any apps that send keybd events whose ExtraInfo is really a pointer value:
  235. #define KEY_IGNORE 0xFFC3D44F
  236. #define KEY_PHYS_IGNORE (KEY_IGNORE - 1)  // Same as above but marked as physical for other instances of the hook.
  237. #define KEY_IGNORE_ALL_EXCEPT_MODIFIER (KEY_IGNORE - 2)  // Non-physical and ignored only if it's not a modifier.
  238.  
  239. // The default in the below is KEY_IGNORE_ALL_EXCEPT_MODIFIER, which causes standard calls to
  240. // KeyEvent() to update g_modifiersLR_logical_non_ignored the same way it updates g_modifiersLR_logical.
  241. // This is done because only the Send command has a realistic chance of interfering with (or being
  242. // interfered with by) hook hotkeys (namely the modifiers used to decide whether to trigger them).
  243. // There are two types of problems:
  244. // 1) Hotkeys not firing due to Send having temporarily released one of that hotkey's modifiers that
  245. //    the user is still holding down.  This causes the hotkey's suffix to flow through to the system,
  246. //    which is usually undesirable.  This happens when the user is holding down a hotkey to auto-repeat
  247. //    it, and perhaps other times.
  248. // 2) The wrong hotkey firing because Send has temporarily put a modifier into effect and (once again)
  249. //    the user is holding down the hotkey to auto-repeat it.  If the Send's temp-down modifier happens
  250. //    to make the hotkey suffix match a different set of modifiers, the wrong hotkey would fire.
  251. void KeyEvent(KeyEventTypes aEventType, vk_type aVK, sc_type aSC = 0, HWND aTargetWindow = NULL
  252.     , bool aDoKeyDelay = false, DWORD aExtraInfo = KEY_IGNORE_ALL_EXCEPT_MODIFIER);
  253.  
  254. ResultType PerformClick(char *aOptions);
  255. void ParseClickOptions(char *aOptions, int &aX, int &aY, vk_type &aVK, KeyEventTypes &aEventType
  256.     , int &aRepeatCount, bool &aMoveOffset);
  257. ResultType PerformMouse(ActionTypeType aActionType, char *aButton, char *aX1, char *aY1, char *aX2, char *aY2
  258.     , char *aSpeed, char *aOffsetMode, char *aRepeatCount = "", char *aDownUp = "");
  259. void PerformMouseCommon(ActionTypeType aActionType, vk_type aVK, int aX1, int aY1, int aX2, int aY2
  260.     , int aRepeatCount, KeyEventTypes aEventType, int aSpeed, bool aMoveOffset);
  261.  
  262. void MouseClickDrag(vk_type aVK // Which button.
  263.     , int aX1, int aY1, int aX2, int aY2, int aSpeed, bool aMoveOffset);
  264. void MouseClick(vk_type aVK // Which button.
  265.     , int aX, int aY, int aRepeatCount, int aSpeed, KeyEventTypes aEventType, bool aMoveOffset = false);
  266. void MouseMove(int &aX, int &aY, DWORD &aEventFlags, int aSpeed, bool aMoveOffset);
  267. void MouseEvent(DWORD aEventFlags, DWORD aData, DWORD aX = COORD_UNSPECIFIED, DWORD aY = COORD_UNSPECIFIED);
  268.  
  269. #define MSG_OFFSET_MOUSE_MOVE 0x80000000  // Bitwise flag, should be near/at high-order bit to avoid overlap messages.
  270. void PutKeybdEventIntoArray(modLR_type aKeyAsModifiersLR, vk_type aVK, sc_type aSC, DWORD aEventFlags, DWORD aExtraInfo);
  271. void PutMouseEventIntoArray(DWORD aEventFlags, DWORD aData, DWORD aX, DWORD aY);
  272. ResultType ExpandEventArray();
  273. void InitEventArray(void *aMem, UINT aMaxEvents, modLR_type aModifiersLR);
  274. void SendEventArray(int &aFinalKeyDelay, modLR_type aModsDuringSend);
  275. void CleanupEventArray(int aFinalKeyDelay);
  276.  
  277. EXTERN_G; // For the DoKeyDelay() prototype below.
  278. extern SendModes sSendMode;
  279. void DoKeyDelay(int aDelay = (sSendMode == SM_PLAY) ? g.KeyDelayPlay : g.KeyDelay);
  280. void DoMouseDelay();
  281. void UpdateKeyEventHistory(bool aKeyUp, vk_type aVK, sc_type aSC);
  282. #define KEYEVENT_PHYS(event_type, vk, sc) KeyEvent(event_type, vk, sc, NULL, false, KEY_PHYS_IGNORE)
  283.  
  284. ToggleValueType ToggleKeyState(vk_type aVK, ToggleValueType aToggleValue);
  285. void ToggleNumlockWin9x();
  286. //void CapslockOffWin9x();
  287.  
  288. #define STD_MODS_TO_DISGUISE (MOD_LALT|MOD_RALT|MOD_LWIN|MOD_RWIN)
  289. void SetModifierLRState(modLR_type aModifiersLRnew, modLR_type aModifiersLRnow, HWND aTargetWindow
  290.     , bool aDisguiseDownWinAlt, bool aDisguiseUpWinAlt, DWORD aExtraInfo = KEY_IGNORE_ALL_EXCEPT_MODIFIER);
  291. modLR_type GetModifierLRState(bool aExplicitlyGet = false);
  292.  
  293. // The IsKeyDown9xNT() method is needed because GetKeyState() does not return the proper
  294. // state under Win9x, at least for the modifier keys under certain conditions.  The
  295. // AutoIt3 author indicates that GetAsyncKeyState() is also unreliable and he uses
  296. // this same method, so it seems best for now.  Specify GetAsyncKeyState() first due
  297. // to performance of short-circuit boolean.  v1.0.42.01: Fixed to use 0x8000 vs. 0x80000000
  298. // with GetAsyncKeyState (though luckily, because of the way a negative short gets promoted
  299. // to a negative int, the old way of using 0x80000000 worked too).
  300. #define IsKeyDown9xNT(vk) (   (GetAsyncKeyState(vk) & 0x8000) || ((GetKeyState(vk) & 0x8000))   )
  301. #define IsKeyDown2kXP(vk) (GetKeyState(vk) & 0x8000)
  302. #define IsKeyDownAsync(vk) (GetAsyncKeyState(vk) & 0x8000)
  303. #define IsKeyToggledOn(vk) (GetKeyState(vk) & 0x01)
  304.  
  305. void AdjustKeyState(BYTE aKeyState[], modLR_type aModifiersLR);
  306. modLR_type KeyToModifiersLR(vk_type aVK, sc_type aSC = 0, bool *pIsNeutral = NULL);
  307. modLR_type ConvertModifiers(mod_type aModifiers);
  308. mod_type ConvertModifiersLR(modLR_type aModifiersLR);
  309. char *ModifiersLRToText(modLR_type aModifiersLR, char *aBuf);
  310.  
  311. #define LAYOUT_UNDETERMINED FAIL
  312. bool ActiveWindowLayoutHasAltGr();
  313. ResultType LayoutHasAltGr(HKL aLayout, ResultType aHasAltGr = LAYOUT_UNDETERMINED);
  314.  
  315. //---------------------------------------------------------------------
  316.  
  317. char *SCtoKeyName(sc_type aSC, char *aBuf, int aBufSize);
  318. char *VKtoKeyName(vk_type aVK, sc_type aSC, char *aBuf, int aBufSize);
  319. sc_type TextToSC(char *aText);
  320. vk_type TextToVK(char *aText, modLR_type *pModifiersLR = NULL, bool aExcludeThoseHandledByScanCode = false
  321.     , bool aAllowExplicitVK = true, HKL aKeybdLayout = GetKeyboardLayout(0));
  322. vk_type CharToVKAndModifiers(char aChar, modLR_type *pModifiersLR, HKL aKeybdLayout);
  323. vk_type TextToSpecial(char *aText, UINT aTextLength, KeyEventTypes &aEventTypem, modLR_type &aModifiersLR
  324.     , bool aUpdatePersistent);
  325.  
  326. #ifdef ENABLE_KEY_HISTORY_FILE
  327. ResultType KeyHistoryToFile(char *aFilespec = NULL, char aType = '\0', bool aKeyUp = false
  328.     , vk_type aVK = 0, sc_type aSC = 0);
  329. #endif
  330.  
  331. char *GetKeyName(vk_type aVK, sc_type aSC, char *aBuf, int aBufSize);
  332. sc_type vk_to_sc(vk_type aVK, bool aReturnSecondary = false);
  333. vk_type sc_to_vk(sc_type aSC);
  334.  
  335. inline bool IsMouseVK(vk_type aVK)
  336. {
  337.     return aVK >= VK_LBUTTON && aVK <= VK_XBUTTON2 && aVK != VK_CANCEL
  338.         || aVK >= VK_NEW_MOUSE_FIRST && aVK <= VK_NEW_MOUSE_LAST;
  339. }
  340.  
  341. #endif
  342.