SendKeys.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. #include "stdafx.h"
  2. #include "sendkeys.h"
  3. /*
  4. * -----------------------------------------------------------------------------
  5. * Copyright (c) 2004 lallous <[email protected]>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. * -----------------------------------------------------------------------------
  29. The Original SendKeys copyright info
  30. ---------------------------------------
  31. SendKeys (sndkeys32.pas) routine for 32-bit Delphi.
  32. Written by Ken Henderson
  33. Copyright (c) 1995 Ken Henderson <[email protected]>
  34. History
  35. ----------
  36. 04/19/2004
  37. * Initial version development
  38. 04/21/2004
  39. * Added number of times specifier to special keys
  40. * Added {BEEP X Y}
  41. * Added {APPACTIVATE WindowTitle}
  42. * Added CarryDelay() and now delay works properly with all keys
  43. * Added SetDelay() method
  44. * Fixed code in AppActivate that allowed to pass both NULL windowTitle/windowClass
  45. 05/21/2004
  46. * Fixed a bug in StringToVKey() that caused the search for RIGHTPAREN to be matched as RIGHT
  47. * Adjusted code so it compiles w/ VC6
  48. 05/24/2004
  49. * Added unicode support
  50. Todo
  51. -------
  52. * perhaps add mousecontrol: mousemove+mouse clicks
  53. * allow sending of normal keys multiple times as: {a 10}
  54. */
  55. const WORD CSendKeys::VKKEYSCANSHIFTON = 0x01;
  56. const WORD CSendKeys::VKKEYSCANCTRLON = 0x02;
  57. const WORD CSendKeys::VKKEYSCANALTON = 0x04;
  58. const WORD CSendKeys::INVALIDKEY = 0xFFFF;
  59. const BYTE CSendKeys::ExtendedVKeys[MaxExtendedVKeys] =
  60. {
  61. VK_UP,
  62. VK_DOWN,
  63. VK_LEFT,
  64. VK_RIGHT,
  65. VK_HOME,
  66. VK_END,
  67. VK_PRIOR, // PgUp
  68. VK_NEXT, // PgDn
  69. VK_INSERT,
  70. VK_DELETE,
  71. VK_CONTROL,
  72. VK_SHIFT,
  73. };
  74. CSendKeys::CSendKeys()
  75. {
  76. m_nDelayNow = m_nDelayAlways = m_keyDownDelay = 0;
  77. }
  78. // Delphi port regexps:
  79. // ---------------------
  80. // search: .+Name:'([^']+)'.+vkey:([^\)]+)\)
  81. // replace: {"\1", \2}
  82. //
  83. // **If you add to this list, you must be sure to keep it sorted alphabetically
  84. // by Name because a binary search routine is used to scan it.**
  85. //
  86. CSendKeys::key_desc_t CSendKeys::KeyNames[CSendKeys::MaxSendKeysRecs] =
  87. {
  88. {_T("ADD"), VK_ADD},
  89. {_T("APPS"), VK_APPS},
  90. {_T("AT"), '@', true},
  91. {_T("BACKSPACE"), VK_BACK},
  92. {_T("BKSP"), VK_BACK},
  93. {_T("BREAK"), VK_CANCEL},
  94. {_T("BS"), VK_BACK},
  95. {_T("CAPSLOCK"), VK_CAPITAL},
  96. {_T("CARET"), '^', true},
  97. {_T("CLEAR"), VK_CLEAR},
  98. {_T("DECIMAL"), VK_DECIMAL},
  99. {_T("DEL"), VK_DELETE},
  100. {_T("DELETE"), VK_DELETE},
  101. {_T("DIVIDE"), VK_DIVIDE},
  102. {_T("DOWN"), VK_DOWN},
  103. {_T("END"), VK_END},
  104. {_T("ENTER"), VK_RETURN},
  105. {_T("ESC"), VK_ESCAPE},
  106. {_T("ESCAPE"), VK_ESCAPE},
  107. {_T("F1"), VK_F1},
  108. {_T("F10"), VK_F10},
  109. {_T("F11"), VK_F11},
  110. {_T("F12"), VK_F12},
  111. {_T("F13"), VK_F13},
  112. {_T("F14"), VK_F14},
  113. {_T("F15"), VK_F15},
  114. {_T("F16"), VK_F16},
  115. {_T("F2"), VK_F2},
  116. {_T("F3"), VK_F3},
  117. {_T("F4"), VK_F4},
  118. {_T("F5"), VK_F5},
  119. {_T("F6"), VK_F6},
  120. {_T("F7"), VK_F7},
  121. {_T("F8"), VK_F8},
  122. {_T("F9"), VK_F9},
  123. {_T("HELP"), VK_HELP},
  124. {_T("HOME"), VK_HOME},
  125. {_T("INS"), VK_INSERT},
  126. {_T("LEFT"), VK_LEFT},
  127. {_T("LEFTBRACE"), '{', true},
  128. {_T("LEFTPAREN"), '(', true},
  129. {_T("LWIN"), VK_LWIN},
  130. {_T("MULTIPLY"), VK_MULTIPLY},
  131. {_T("NUMLOCK"), VK_NUMLOCK},
  132. {_T("NUMPAD0"), VK_NUMPAD0},
  133. {_T("NUMPAD1"), VK_NUMPAD1},
  134. {_T("NUMPAD2"), VK_NUMPAD2},
  135. {_T("NUMPAD3"), VK_NUMPAD3},
  136. {_T("NUMPAD4"), VK_NUMPAD4},
  137. {_T("NUMPAD5"), VK_NUMPAD5},
  138. {_T("NUMPAD6"), VK_NUMPAD6},
  139. {_T("NUMPAD7"), VK_NUMPAD7},
  140. {_T("NUMPAD8"), VK_NUMPAD8},
  141. {_T("NUMPAD9"), VK_NUMPAD9},
  142. {_T("PERCENT"), '%', true},
  143. {_T("PGDN"), VK_NEXT},
  144. {_T("PGUP"), VK_PRIOR},
  145. {_T("PLUS"), '+', true},
  146. {_T("PRTSC"), VK_PRINT},
  147. {_T("RIGHT"), VK_RIGHT},
  148. {_T("RIGHTBRACE"), '}', true},
  149. {_T("RIGHTPAREN"), ')', true},
  150. {_T("RWIN"), VK_RWIN},
  151. {_T("SCROLL"), VK_SCROLL},
  152. {_T("SEPARATOR"), VK_SEPARATOR},
  153. {_T("SNAPSHOT"), VK_SNAPSHOT},
  154. {_T("SUBTRACT"), VK_SUBTRACT},
  155. {_T("TAB"), VK_TAB},
  156. {_T("TILDE"), '~', true},
  157. {_T("UP"), VK_UP},
  158. {_T("WIN"), VK_LWIN}
  159. };
  160. // calls keybd_event() and waits, if needed, till the sent input is processed
  161. void CSendKeys::KeyboardEvent(BYTE VKey, BYTE ScanCode, LONG Flags)
  162. {
  163. keybd_event(VKey, ScanCode, Flags, 0);
  164. if (m_bWait)
  165. {
  166. MSG KeyboardMsg;
  167. while (::PeekMessage(&KeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
  168. {
  169. ::TranslateMessage(&KeyboardMsg);
  170. ::DispatchMessage(&KeyboardMsg);
  171. }
  172. }
  173. }
  174. // Checks whether the specified VKey is an extended key or not
  175. bool CSendKeys::IsVkExtended(BYTE VKey)
  176. {
  177. for (int i=0;i<MaxExtendedVKeys;i++)
  178. {
  179. if (ExtendedVKeys[i] == VKey)
  180. return true;
  181. }
  182. return false;
  183. }
  184. CString CSendKeys::VkString(BYTE VKey)
  185. {
  186. for (int i = 0; i < MaxSendKeysRecs; i++)
  187. {
  188. if (KeyNames[i].VKey == VKey)
  189. return KeyNames[i].keyName;
  190. }
  191. return _T("");
  192. }
  193. void CSendKeys::AllKeysUp()
  194. {
  195. for (int key = 0; key < 256; key++)
  196. {
  197. //If the key is pressed, send a key up, having other keys down interferes with sending ctrl-v, -c and -x
  198. short val = GetKeyState(key);
  199. if (val & 0x8000)
  200. {
  201. SendKeyUp(key);
  202. //CString cs;
  203. //cs.Format(_T("key was down, sending key up %d, value: %d\n"), key, val);
  204. //OutputDebugString(cs);
  205. }
  206. }
  207. //Force these to be up, i was having trouble with the right shift key, i think things were
  208. //getting confused between the basic shift and lshift, but not sure.
  209. SendKeyUp(VK_LSHIFT);
  210. SendKeyUp(VK_RSHIFT);
  211. SendKeyUp(VK_LCONTROL);
  212. SendKeyUp(VK_RCONTROL);
  213. SendKeyUp(VK_LMENU);
  214. SendKeyUp(VK_RMENU);
  215. }
  216. // Generates KEYUP
  217. void CSendKeys::SendKeyUp(BYTE VKey)
  218. {
  219. BYTE ScanCode = LOBYTE(::MapVirtualKey(VKey, 0));
  220. KeyboardEvent(VKey,
  221. ScanCode,
  222. KEYEVENTF_KEYUP | (IsVkExtended(VKey) ? KEYEVENTF_EXTENDEDKEY : 0));
  223. }
  224. void CSendKeys::SendKeyDown(BYTE VKey, WORD NumTimes, bool GenUpMsg, bool bDelay)
  225. {
  226. WORD Cnt = 0;
  227. BYTE ScanCode = 0;
  228. bool NumState = false;
  229. if (VKey == VK_NUMLOCK)
  230. {
  231. DWORD dwVersion = ::GetVersion();
  232. for (Cnt=1; Cnt<=NumTimes; Cnt++)
  233. {
  234. if (bDelay)
  235. CarryDelay();
  236. // snippet based on:
  237. // http://www.codeproject.com/cpp/togglekeys.asp
  238. if (dwVersion < 0x80000000)
  239. {
  240. ::keybd_event(VKey, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
  241. ::keybd_event(VKey, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  242. }
  243. else
  244. {
  245. // Win98 and later
  246. if ( ((DWORD)(HIBYTE(LOWORD(dwVersion))) >= 10) )
  247. {
  248. // Define _WIN32_WINNT > 0x0400
  249. // to compile
  250. INPUT input[2] = {0};
  251. input[0].type = input[1].type = INPUT_KEYBOARD;
  252. input[0].ki.wVk = input[1].ki.wVk = VKey;
  253. input[1].ki.dwFlags = KEYEVENTF_KEYUP;
  254. ::SendInput(sizeof(input) / sizeof(INPUT), input, sizeof(INPUT));
  255. }
  256. // Win95
  257. else
  258. {
  259. KEYBOARDSTATE_t KeyboardState;
  260. NumState = GetKeyState(VK_NUMLOCK) & 1 ? true : false;
  261. GetKeyboardState(&KeyboardState[0]);
  262. if (NumState)
  263. KeyboardState[VK_NUMLOCK] &= ~1;
  264. else
  265. KeyboardState[VK_NUMLOCK] |= 1;
  266. SetKeyboardState(&KeyboardState[0]);
  267. }
  268. }
  269. }
  270. return;
  271. }
  272. // Get scancode
  273. ScanCode = LOBYTE(::MapVirtualKey(VKey, 0));
  274. // Send keys
  275. for (Cnt=1; Cnt<=NumTimes; Cnt++)
  276. {
  277. // Carry needed delay ?
  278. if (bDelay)
  279. CarryDelay();
  280. KeyboardEvent(VKey, ScanCode, IsVkExtended(VKey) ? KEYEVENTF_EXTENDEDKEY : 0);
  281. if(m_keyDownDelay > 0)
  282. {
  283. Sleep(m_keyDownDelay);
  284. }
  285. if (GenUpMsg)
  286. {
  287. SendKeyUp(VKey);
  288. }
  289. }
  290. }
  291. // Checks whether a bit is set
  292. bool CSendKeys::BitSet(BYTE BitTable, UINT BitMask)
  293. {
  294. return BitTable & BitMask ? true : false;
  295. }
  296. // Sends a single key
  297. void CSendKeys::SendKey(WORD MKey, WORD NumTimes, bool GenDownMsg)
  298. {
  299. // Send appropriate shift keys associated with the given VKey
  300. if (BitSet(HIBYTE(MKey), VKKEYSCANSHIFTON))
  301. SendKeyDown(VK_SHIFT, 1, false);
  302. if (BitSet(HIBYTE(MKey), VKKEYSCANCTRLON))
  303. SendKeyDown(VK_CONTROL, 1, false);
  304. if (BitSet(HIBYTE(MKey), VKKEYSCANALTON))
  305. SendKeyDown(VK_MENU, 1, false);
  306. // Send the actual VKey
  307. SendKeyDown(LOBYTE(MKey), NumTimes, GenDownMsg, true);
  308. // toggle up shift keys
  309. if (BitSet(HIBYTE(MKey), VKKEYSCANSHIFTON))
  310. SendKeyUp(VK_SHIFT);
  311. if (BitSet(HIBYTE(MKey), VKKEYSCANCTRLON))
  312. SendKeyUp(VK_CONTROL);
  313. if (BitSet(HIBYTE(MKey), VKKEYSCANALTON))
  314. SendKeyUp(VK_MENU);
  315. }
  316. // Implements a simple binary search to locate special key name strings
  317. WORD CSendKeys::StringToVKey(LPCTSTR KeyString, int &idx)
  318. {
  319. bool Found = false, Collided;
  320. int Bottom = 0,
  321. Top = MaxSendKeysRecs,
  322. Middle = (Bottom + Top) / 2;
  323. WORD retval = INVALIDKEY;
  324. idx = -1;
  325. do
  326. {
  327. Collided = (Bottom == Middle) || (Top == Middle);
  328. int cmp = _tcsnicmp(KeyNames[Middle].keyName, KeyString, _tcslen(KeyString));
  329. if (cmp == 0)
  330. {
  331. Found = true;
  332. retval = KeyNames[Middle].VKey;
  333. idx = Middle;
  334. break;
  335. }
  336. else
  337. {
  338. if (cmp < 0)
  339. Bottom = Middle;
  340. else
  341. Top = Middle;
  342. Middle = (Bottom + Top) / 2;
  343. }
  344. } while (!(Found || Collided));
  345. return retval;
  346. }
  347. // Releases all shift keys (keys that can be depressed while other keys are being pressed
  348. // If we are in a modifier group this function does nothing
  349. void CSendKeys::PopUpShiftKeys()
  350. {
  351. if (!m_bUsingParens)
  352. {
  353. if (m_bShiftDown)
  354. {
  355. SendKeyUp(VK_SHIFT);
  356. }
  357. if (m_bControlDown)
  358. {
  359. SendKeyUp(VK_CONTROL);
  360. }
  361. if (m_bAltDown)
  362. {
  363. SendKeyUp(VK_MENU);
  364. }
  365. if (m_bWinDown)
  366. {
  367. SendKeyUp(VK_LWIN);
  368. }
  369. m_bWinDown = m_bShiftDown = m_bControlDown = m_bAltDown = false;
  370. }
  371. }
  372. // Sends a key string
  373. bool CSendKeys::SendKeys(LPCTSTR KeysString, bool Wait)
  374. {
  375. WORD MKey;
  376. WORD NumTimes = 1;
  377. TCHAR KeyString[300] = {0};
  378. bool retval = false;
  379. int keyIdx;
  380. LPTSTR pKey = (LPTSTR) KeysString;
  381. TCHAR ch;
  382. m_bWait = Wait;
  383. m_bWinDown = m_bShiftDown = m_bControlDown = m_bAltDown = m_bUsingParens = false;
  384. while (ch = *pKey)
  385. {
  386. switch (ch)
  387. {
  388. // begin modifier group
  389. case _TXCHAR('('):
  390. m_bUsingParens = true;
  391. break;
  392. // end modifier group
  393. case _TXCHAR(')'):
  394. m_bUsingParens = false;
  395. PopUpShiftKeys(); // pop all shift keys when we finish a modifier group close
  396. break;
  397. // ALT key
  398. case _TXCHAR('%'):
  399. m_bAltDown = true;
  400. SendKeyDown(VK_MENU, 1, false);
  401. break;
  402. // SHIFT key
  403. case _TXCHAR('+'):
  404. m_bShiftDown = true;
  405. SendKeyDown(VK_SHIFT, 1, false);
  406. break;
  407. // CTRL key
  408. case _TXCHAR('^'):
  409. m_bControlDown = true;
  410. SendKeyDown(VK_CONTROL, 1, false);
  411. break;
  412. // WINKEY (Left-WinKey)
  413. case '@':
  414. m_bWinDown = true;
  415. SendKeyDown(VK_LWIN, 1, false);
  416. break;
  417. // enter
  418. case _TXCHAR('~'):
  419. SendKeyDown(VK_RETURN, 1, true);
  420. PopUpShiftKeys();
  421. break;
  422. // begin special keys
  423. case _TXCHAR('{'):
  424. {
  425. LPTSTR p = pKey+1; // skip past the beginning '{'
  426. size_t t;
  427. // find end of close
  428. while (*p && *p != _TXCHAR('}'))
  429. p++;
  430. t = p - pKey;
  431. // special key definition too big?
  432. if (t > sizeof(KeyString))
  433. return false;
  434. // Take this KeyString into local buffer
  435. _tcsncpy(KeyString, pKey+1, t);
  436. KeyString[t-1] = _TXCHAR('\0');
  437. keyIdx = -1;
  438. pKey += t; // skip to next keystring
  439. // Invalidate key
  440. MKey = INVALIDKEY;
  441. // sending arbitrary vkeys?
  442. if (_tcsnicmp(KeyString, _T("VKEY"), 4) == 0)
  443. {
  444. p = KeyString + 4;
  445. MKey = _ttoi(p);
  446. }
  447. else if (_tcsnicmp(KeyString, _T("BEEP"), 4) == 0)
  448. {
  449. p = KeyString + 4 + 1;
  450. LPTSTR p1 = p;
  451. DWORD frequency, delay;
  452. if ((p1 = _tcsstr(p, _T(" "))) != NULL)
  453. {
  454. *p1++ = _TXCHAR('\0');
  455. frequency = _ttoi(p);
  456. delay = _ttoi(p1);
  457. ::Beep(frequency, delay);
  458. }
  459. }
  460. // Should activate a window?
  461. else if (_tcsnicmp(KeyString, _T("APPACTIVATE"), 11) == 0)
  462. {
  463. p = KeyString + 11 + 1;
  464. AppActivate(p);
  465. }
  466. // want to send/set delay?
  467. else if (_tcsnicmp(KeyString, _T("DELAY"), 5) == 0)
  468. {
  469. // Advance to parameters
  470. p = KeyString + 5;
  471. // set "sleep factor"
  472. if (*p == _TXCHAR('='))
  473. m_nDelayAlways = _ttoi(p + 1); // Take number after the '=' character
  474. else
  475. // set "sleep now"
  476. m_nDelayNow = _ttoi(p);
  477. }
  478. // not command special keys, then process as keystring to VKey
  479. else
  480. {
  481. MKey = StringToVKey(KeyString, keyIdx);
  482. // Key found in table
  483. if (keyIdx != -1)
  484. {
  485. NumTimes = 1;
  486. // Does the key string have also count specifier?
  487. t = _tcslen(KeyNames[keyIdx].keyName);
  488. if (_tcslen(KeyString) > t)
  489. {
  490. p = KeyString + t;
  491. // Take the specified number of times
  492. NumTimes = _ttoi(p);
  493. }
  494. if (KeyNames[keyIdx].normalkey)
  495. MKey = ::VkKeyScan(KeyNames[keyIdx].VKey);
  496. }
  497. }
  498. // A valid key to send?
  499. if (MKey != INVALIDKEY)
  500. {
  501. SendKey(MKey, NumTimes, true);
  502. PopUpShiftKeys();
  503. }
  504. }
  505. break;
  506. // a normal key was pressed
  507. default:
  508. // Get the VKey from the key
  509. MKey = ::VkKeyScan(ch);
  510. SendKey(MKey, 1, true);
  511. PopUpShiftKeys();
  512. }
  513. pKey++;
  514. }
  515. m_bUsingParens = false;
  516. PopUpShiftKeys();
  517. return true;
  518. }
  519. bool CSendKeys::AppActivate(HWND wnd)
  520. {
  521. if (wnd == NULL)
  522. return false;
  523. ::SendMessage(wnd, WM_SYSCOMMAND, SC_HOTKEY, (LPARAM) wnd);
  524. ::SendMessage(wnd, WM_SYSCOMMAND, SC_RESTORE, (LPARAM) wnd);
  525. ::ShowWindow(wnd, SW_SHOW);
  526. ::SetForegroundWindow(wnd);
  527. ::SetFocus(wnd);
  528. return true;
  529. }
  530. BOOL CALLBACK CSendKeys::enumwindowsProc(HWND hwnd, LPARAM lParam)
  531. {
  532. enumwindow_t *t = (enumwindow_t *) lParam;
  533. LPTSTR wtitle = 0, wclass = 0, str = t->str;
  534. if (!*str)
  535. str++;
  536. else
  537. {
  538. wtitle = str;
  539. str += _tcslen(str) + 1;
  540. }
  541. if (*str)
  542. wclass = str;
  543. bool bMatch(false);
  544. if (wclass)
  545. {
  546. TCHAR szClass[300];
  547. if (::GetClassName(hwnd, szClass, sizeof(szClass)))
  548. bMatch |= (_tcsstr(szClass, wclass) != 0);
  549. }
  550. if (wtitle)
  551. {
  552. TCHAR szTitle[300];
  553. if (::GetWindowText(hwnd, szTitle, sizeof(szTitle)))
  554. bMatch |= (_tcsstr(szTitle, wtitle) != 0);
  555. }
  556. if (bMatch)
  557. {
  558. t->hwnd = hwnd;
  559. return false;
  560. }
  561. return true;
  562. }
  563. // Searchs and activates a window given its title or class name
  564. bool CSendKeys::AppActivate(LPCTSTR WindowTitle, LPCTSTR WindowClass)
  565. {
  566. HWND w;
  567. w = ::FindWindow(WindowClass, WindowTitle);
  568. if (w == NULL)
  569. {
  570. // Must specify at least a criteria
  571. if (WindowTitle == NULL && WindowClass == NULL)
  572. return false;
  573. // << Code to serialize the windowtitle/windowclass in order to send to EnumWindowProc()
  574. size_t l1(0), l2(0);
  575. if (WindowTitle)
  576. l1 = _tcslen(WindowTitle);
  577. if (WindowClass)
  578. l2 = _tcslen(WindowClass);
  579. LPTSTR titleclass = new TCHAR [l1 + l2 + 5];
  580. memset(titleclass, '\0', l1+l2+5);
  581. if (WindowTitle)
  582. _tcscpy(titleclass, WindowTitle);
  583. titleclass[l1] = 0;
  584. if (WindowClass)
  585. _tcscpy(titleclass+l1+1, WindowClass);
  586. // >>
  587. enumwindow_t t;
  588. t.hwnd = NULL;
  589. t.str = titleclass;
  590. ::EnumWindows(enumwindowsProc, (LPARAM) & t);
  591. w = t.hwnd;
  592. delete [] titleclass;
  593. }
  594. if (w == NULL)
  595. return false;
  596. return AppActivate(w);
  597. }
  598. // Carries the required delay and clears the m_nDelaynow value
  599. void CSendKeys::CarryDelay()
  600. {
  601. // Should we delay once?
  602. if (!m_nDelayNow)
  603. // should we delay always?
  604. m_nDelayNow = m_nDelayAlways;
  605. // No delay specified?
  606. if (m_nDelayNow)
  607. ::Sleep(m_nDelayNow); //::Beep(100, m_nDelayNow);
  608. // clear SleepNow
  609. m_nDelayNow = 0;
  610. }
  611. /*
  612. Test Binary search
  613. void CSendKeys::test()
  614. {
  615. WORD miss(0);
  616. for (int i=0;i<MaxSendKeysRecs;i++)
  617. {
  618. char *p = (char *)KeyNames[i].keyName;
  619. WORD v = StringToVKeyB(p);
  620. if (v == INVALIDKEY)
  621. {
  622. miss++;
  623. }
  624. }
  625. }
  626. */
  627. /*
  628. Search in a linear manner
  629. WORD CSendKeys::StringToVKey(const char *KeyString, int &idx)
  630. {
  631. for (int i=0;i<MaxSendKeysRecs;i++)
  632. {
  633. size_t len = strlen(KeyNames[i].keyName);
  634. if (strnicmp(KeyNames[i].keyName, KeyString, len) == 0)
  635. {
  636. idx = i;
  637. return KeyNames[i].VKey;
  638. }
  639. }
  640. idx = -1;
  641. return INVALIDKEY;
  642. }
  643. */