SendKeys.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. void CSendKeys::AllKeysUp()
  185. {
  186. for(int key = 0; key < 256; key++)
  187. {
  188. //If the key is pressed, send a key up, having other keys down interferes with sending ctrl-v, -c and -x
  189. if(GetKeyState(key) & 0x8000)
  190. {
  191. SendKeyUp(key);
  192. }
  193. }
  194. }
  195. // Generates KEYUP
  196. void CSendKeys::SendKeyUp(BYTE VKey)
  197. {
  198. BYTE ScanCode = LOBYTE(::MapVirtualKey(VKey, 0));
  199. KeyboardEvent(VKey,
  200. ScanCode,
  201. KEYEVENTF_KEYUP | (IsVkExtended(VKey) ? KEYEVENTF_EXTENDEDKEY : 0));
  202. }
  203. void CSendKeys::SendKeyDown(BYTE VKey, WORD NumTimes, bool GenUpMsg, bool bDelay)
  204. {
  205. WORD Cnt = 0;
  206. BYTE ScanCode = 0;
  207. bool NumState = false;
  208. if (VKey == VK_NUMLOCK)
  209. {
  210. DWORD dwVersion = ::GetVersion();
  211. for (Cnt=1; Cnt<=NumTimes; Cnt++)
  212. {
  213. if (bDelay)
  214. CarryDelay();
  215. // snippet based on:
  216. // http://www.codeproject.com/cpp/togglekeys.asp
  217. if (dwVersion < 0x80000000)
  218. {
  219. ::keybd_event(VKey, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
  220. ::keybd_event(VKey, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  221. }
  222. else
  223. {
  224. // Win98 and later
  225. if ( ((DWORD)(HIBYTE(LOWORD(dwVersion))) >= 10) )
  226. {
  227. // Define _WIN32_WINNT > 0x0400
  228. // to compile
  229. INPUT input[2] = {0};
  230. input[0].type = input[1].type = INPUT_KEYBOARD;
  231. input[0].ki.wVk = input[1].ki.wVk = VKey;
  232. input[1].ki.dwFlags = KEYEVENTF_KEYUP;
  233. ::SendInput(sizeof(input) / sizeof(INPUT), input, sizeof(INPUT));
  234. }
  235. // Win95
  236. else
  237. {
  238. KEYBOARDSTATE_t KeyboardState;
  239. NumState = GetKeyState(VK_NUMLOCK) & 1 ? true : false;
  240. GetKeyboardState(&KeyboardState[0]);
  241. if (NumState)
  242. KeyboardState[VK_NUMLOCK] &= ~1;
  243. else
  244. KeyboardState[VK_NUMLOCK] |= 1;
  245. SetKeyboardState(&KeyboardState[0]);
  246. }
  247. }
  248. }
  249. return;
  250. }
  251. // Get scancode
  252. ScanCode = LOBYTE(::MapVirtualKey(VKey, 0));
  253. // Send keys
  254. for (Cnt=1; Cnt<=NumTimes; Cnt++)
  255. {
  256. // Carry needed delay ?
  257. if (bDelay)
  258. CarryDelay();
  259. KeyboardEvent(VKey, ScanCode, IsVkExtended(VKey) ? KEYEVENTF_EXTENDEDKEY : 0);
  260. if(m_keyDownDelay > 0)
  261. {
  262. Sleep(m_keyDownDelay);
  263. }
  264. if (GenUpMsg)
  265. KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP);
  266. }
  267. }
  268. // Checks whether a bit is set
  269. bool CSendKeys::BitSet(BYTE BitTable, UINT BitMask)
  270. {
  271. return BitTable & BitMask ? true : false;
  272. }
  273. // Sends a single key
  274. void CSendKeys::SendKey(WORD MKey, WORD NumTimes, bool GenDownMsg)
  275. {
  276. // Send appropriate shift keys associated with the given VKey
  277. if (BitSet(HIBYTE(MKey), VKKEYSCANSHIFTON))
  278. SendKeyDown(VK_SHIFT, 1, false);
  279. if (BitSet(HIBYTE(MKey), VKKEYSCANCTRLON))
  280. SendKeyDown(VK_CONTROL, 1, false);
  281. if (BitSet(HIBYTE(MKey), VKKEYSCANALTON))
  282. SendKeyDown(VK_MENU, 1, false);
  283. // Send the actual VKey
  284. SendKeyDown(LOBYTE(MKey), NumTimes, GenDownMsg, true);
  285. // toggle up shift keys
  286. if (BitSet(HIBYTE(MKey), VKKEYSCANSHIFTON))
  287. SendKeyUp(VK_SHIFT);
  288. if (BitSet(HIBYTE(MKey), VKKEYSCANCTRLON))
  289. SendKeyUp(VK_CONTROL);
  290. if (BitSet(HIBYTE(MKey), VKKEYSCANALTON))
  291. SendKeyUp(VK_MENU);
  292. }
  293. // Implements a simple binary search to locate special key name strings
  294. WORD CSendKeys::StringToVKey(LPCTSTR KeyString, int &idx)
  295. {
  296. bool Found = false, Collided;
  297. int Bottom = 0,
  298. Top = MaxSendKeysRecs,
  299. Middle = (Bottom + Top) / 2;
  300. WORD retval = INVALIDKEY;
  301. idx = -1;
  302. do
  303. {
  304. Collided = (Bottom == Middle) || (Top == Middle);
  305. int cmp = _tcsnicmp(KeyNames[Middle].keyName, KeyString, _tcslen(KeyString));
  306. if (cmp == 0)
  307. {
  308. Found = true;
  309. retval = KeyNames[Middle].VKey;
  310. idx = Middle;
  311. break;
  312. }
  313. else
  314. {
  315. if (cmp < 0)
  316. Bottom = Middle;
  317. else
  318. Top = Middle;
  319. Middle = (Bottom + Top) / 2;
  320. }
  321. } while (!(Found || Collided));
  322. return retval;
  323. }
  324. // Releases all shift keys (keys that can be depressed while other keys are being pressed
  325. // If we are in a modifier group this function does nothing
  326. void CSendKeys::PopUpShiftKeys()
  327. {
  328. if (!m_bUsingParens)
  329. {
  330. if (m_bShiftDown)
  331. {
  332. SendKeyUp(VK_SHIFT);
  333. }
  334. if (m_bControlDown)
  335. {
  336. SendKeyUp(VK_CONTROL);
  337. }
  338. if (m_bAltDown)
  339. {
  340. SendKeyUp(VK_MENU);
  341. }
  342. if (m_bWinDown)
  343. {
  344. SendKeyUp(VK_LWIN);
  345. }
  346. m_bWinDown = m_bShiftDown = m_bControlDown = m_bAltDown = false;
  347. }
  348. }
  349. // Sends a key string
  350. bool CSendKeys::SendKeys(LPCTSTR KeysString, bool Wait)
  351. {
  352. WORD MKey, NumTimes;
  353. TCHAR KeyString[300] = {0};
  354. bool retval = false;
  355. int keyIdx;
  356. LPTSTR pKey = (LPTSTR) KeysString;
  357. TCHAR ch;
  358. m_bWait = Wait;
  359. m_bWinDown = m_bShiftDown = m_bControlDown = m_bAltDown = m_bUsingParens = false;
  360. while (ch = *pKey)
  361. {
  362. switch (ch)
  363. {
  364. // begin modifier group
  365. case _TXCHAR('('):
  366. m_bUsingParens = true;
  367. break;
  368. // end modifier group
  369. case _TXCHAR(')'):
  370. m_bUsingParens = false;
  371. PopUpShiftKeys(); // pop all shift keys when we finish a modifier group close
  372. break;
  373. // ALT key
  374. case _TXCHAR('%'):
  375. m_bAltDown = true;
  376. SendKeyDown(VK_MENU, 1, false);
  377. break;
  378. // SHIFT key
  379. case _TXCHAR('+'):
  380. m_bShiftDown = true;
  381. SendKeyDown(VK_SHIFT, 1, false);
  382. break;
  383. // CTRL key
  384. case _TXCHAR('^'):
  385. m_bControlDown = true;
  386. SendKeyDown(VK_CONTROL, 1, false);
  387. break;
  388. // WINKEY (Left-WinKey)
  389. case '@':
  390. m_bWinDown = true;
  391. SendKeyDown(VK_LWIN, 1, false);
  392. break;
  393. // enter
  394. case _TXCHAR('~'):
  395. SendKeyDown(VK_RETURN, 1, true);
  396. PopUpShiftKeys();
  397. break;
  398. // begin special keys
  399. case _TXCHAR('{'):
  400. {
  401. LPTSTR p = pKey+1; // skip past the beginning '{'
  402. size_t t;
  403. // find end of close
  404. while (*p && *p != _TXCHAR('}'))
  405. p++;
  406. t = p - pKey;
  407. // special key definition too big?
  408. if (t > sizeof(KeyString))
  409. return false;
  410. // Take this KeyString into local buffer
  411. _tcsncpy(KeyString, pKey+1, t);
  412. KeyString[t-1] = _TXCHAR('\0');
  413. keyIdx = -1;
  414. pKey += t; // skip to next keystring
  415. // Invalidate key
  416. MKey = INVALIDKEY;
  417. // sending arbitrary vkeys?
  418. if (_tcsnicmp(KeyString, _T("VKEY"), 4) == 0)
  419. {
  420. p = KeyString + 4;
  421. MKey = _ttoi(p);
  422. }
  423. else if (_tcsnicmp(KeyString, _T("BEEP"), 4) == 0)
  424. {
  425. p = KeyString + 4 + 1;
  426. LPTSTR p1 = p;
  427. DWORD frequency, delay;
  428. if ((p1 = _tcsstr(p, _T(" "))) != NULL)
  429. {
  430. *p1++ = _TXCHAR('\0');
  431. frequency = _ttoi(p);
  432. delay = _ttoi(p1);
  433. ::Beep(frequency, delay);
  434. }
  435. }
  436. // Should activate a window?
  437. else if (_tcsnicmp(KeyString, _T("APPACTIVATE"), 11) == 0)
  438. {
  439. p = KeyString + 11 + 1;
  440. AppActivate(p);
  441. }
  442. // want to send/set delay?
  443. else if (_tcsnicmp(KeyString, _T("DELAY"), 5) == 0)
  444. {
  445. // Advance to parameters
  446. p = KeyString + 5;
  447. // set "sleep factor"
  448. if (*p == _TXCHAR('='))
  449. m_nDelayAlways = _ttoi(p + 1); // Take number after the '=' character
  450. else
  451. // set "sleep now"
  452. m_nDelayNow = _ttoi(p);
  453. }
  454. // not command special keys, then process as keystring to VKey
  455. else
  456. {
  457. MKey = StringToVKey(KeyString, keyIdx);
  458. // Key found in table
  459. if (keyIdx != -1)
  460. {
  461. NumTimes = 1;
  462. // Does the key string have also count specifier?
  463. t = _tcslen(KeyNames[keyIdx].keyName);
  464. if (_tcslen(KeyString) > t)
  465. {
  466. p = KeyString + t;
  467. // Take the specified number of times
  468. NumTimes = _ttoi(p);
  469. }
  470. if (KeyNames[keyIdx].normalkey)
  471. MKey = ::VkKeyScan(KeyNames[keyIdx].VKey);
  472. }
  473. }
  474. // A valid key to send?
  475. if (MKey != INVALIDKEY)
  476. {
  477. SendKey(MKey, NumTimes, true);
  478. PopUpShiftKeys();
  479. }
  480. }
  481. break;
  482. // a normal key was pressed
  483. default:
  484. // Get the VKey from the key
  485. MKey = ::VkKeyScan(ch);
  486. SendKey(MKey, 1, true);
  487. PopUpShiftKeys();
  488. }
  489. pKey++;
  490. }
  491. m_bUsingParens = false;
  492. PopUpShiftKeys();
  493. return true;
  494. }
  495. bool CSendKeys::AppActivate(HWND wnd)
  496. {
  497. if (wnd == NULL)
  498. return false;
  499. ::SendMessage(wnd, WM_SYSCOMMAND, SC_HOTKEY, (LPARAM) wnd);
  500. ::SendMessage(wnd, WM_SYSCOMMAND, SC_RESTORE, (LPARAM) wnd);
  501. ::ShowWindow(wnd, SW_SHOW);
  502. ::SetForegroundWindow(wnd);
  503. ::SetFocus(wnd);
  504. return true;
  505. }
  506. BOOL CALLBACK CSendKeys::enumwindowsProc(HWND hwnd, LPARAM lParam)
  507. {
  508. enumwindow_t *t = (enumwindow_t *) lParam;
  509. LPTSTR wtitle = 0, wclass = 0, str = t->str;
  510. if (!*str)
  511. str++;
  512. else
  513. {
  514. wtitle = str;
  515. str += _tcslen(str) + 1;
  516. }
  517. if (*str)
  518. wclass = str;
  519. bool bMatch(false);
  520. if (wclass)
  521. {
  522. TCHAR szClass[300];
  523. if (::GetClassName(hwnd, szClass, sizeof(szClass)))
  524. bMatch |= (_tcsstr(szClass, wclass) != 0);
  525. }
  526. if (wtitle)
  527. {
  528. TCHAR szTitle[300];
  529. if (::GetWindowText(hwnd, szTitle, sizeof(szTitle)))
  530. bMatch |= (_tcsstr(szTitle, wtitle) != 0);
  531. }
  532. if (bMatch)
  533. {
  534. t->hwnd = hwnd;
  535. return false;
  536. }
  537. return true;
  538. }
  539. // Searchs and activates a window given its title or class name
  540. bool CSendKeys::AppActivate(LPCTSTR WindowTitle, LPCTSTR WindowClass)
  541. {
  542. HWND w;
  543. w = ::FindWindow(WindowClass, WindowTitle);
  544. if (w == NULL)
  545. {
  546. // Must specify at least a criteria
  547. if (WindowTitle == NULL && WindowClass == NULL)
  548. return false;
  549. // << Code to serialize the windowtitle/windowclass in order to send to EnumWindowProc()
  550. size_t l1(0), l2(0);
  551. if (WindowTitle)
  552. l1 = _tcslen(WindowTitle);
  553. if (WindowClass)
  554. l2 = _tcslen(WindowClass);
  555. LPTSTR titleclass = new TCHAR [l1 + l2 + 5];
  556. memset(titleclass, '\0', l1+l2+5);
  557. if (WindowTitle)
  558. _tcscpy(titleclass, WindowTitle);
  559. titleclass[l1] = 0;
  560. if (WindowClass)
  561. _tcscpy(titleclass+l1+1, WindowClass);
  562. // >>
  563. enumwindow_t t;
  564. t.hwnd = NULL;
  565. t.str = titleclass;
  566. ::EnumWindows(enumwindowsProc, (LPARAM) & t);
  567. w = t.hwnd;
  568. delete [] titleclass;
  569. }
  570. if (w == NULL)
  571. return false;
  572. return AppActivate(w);
  573. }
  574. // Carries the required delay and clears the m_nDelaynow value
  575. void CSendKeys::CarryDelay()
  576. {
  577. // Should we delay once?
  578. if (!m_nDelayNow)
  579. // should we delay always?
  580. m_nDelayNow = m_nDelayAlways;
  581. // No delay specified?
  582. if (m_nDelayNow)
  583. ::Sleep(m_nDelayNow); //::Beep(100, m_nDelayNow);
  584. // clear SleepNow
  585. m_nDelayNow = 0;
  586. }
  587. /*
  588. Test Binary search
  589. void CSendKeys::test()
  590. {
  591. WORD miss(0);
  592. for (int i=0;i<MaxSendKeysRecs;i++)
  593. {
  594. char *p = (char *)KeyNames[i].keyName;
  595. WORD v = StringToVKeyB(p);
  596. if (v == INVALIDKEY)
  597. {
  598. miss++;
  599. }
  600. }
  601. }
  602. */
  603. /*
  604. Search in a linear manner
  605. WORD CSendKeys::StringToVKey(const char *KeyString, int &idx)
  606. {
  607. for (int i=0;i<MaxSendKeysRecs;i++)
  608. {
  609. size_t len = strlen(KeyNames[i].keyName);
  610. if (strnicmp(KeyNames[i].keyName, KeyString, len) == 0)
  611. {
  612. idx = i;
  613. return KeyNames[i].VKey;
  614. }
  615. }
  616. idx = -1;
  617. return INVALIDKEY;
  618. }
  619. */