1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88:
| procedure MakeWindowActive(wHandle: hWnd); function GetHandleFromWindowTitle(TitleText: string): hWnd;
implementation
procedure SendKeyString(Text: string); var i: Integer; Shift: Boolean; vk, ScanCode: Word; ch: Char; c, s: Byte; const vk_keys: array[0..9] of Byte = (VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE); vk_shft: array[0..2] of Byte = (VK_SHIFT, VK_CONTROL, VK_MENU); flags: array[False..True] of Integer = (KEYEVENTF_KEYUP, 0); begin Shift := False; for i := 1 to Length(Text) do begin ch := Text[i]; if ch >= #250 then begin s := Ord(ch) - 250; Shift := not Odd(s); c := vk_shft[s shr 1]; ScanCode := MapVirtualKey(c, 0); Keybd_Event(c, Scancode, Flags[shift], 0); end else begin vk := 0; if ch >= #240 then c := vk_keys[Ord(ch) - 240] else if ch >= #228 then c := Ord(ch) - 116 else if ch < #110 then c := Ord(ch) else begin vk := VkKeyScan(ch); c := LoByte(vk); end; ScanCode := MapVirtualKey(c, 0); if not Shift and (Hi(vk) > 0) then Keybd_Event(VK_SHIFT, $2A, 0, 0); Keybd_Event(c, scancode, 0, 0); Keybd_Event(c, scancode, KEYEVENTF_KEYUP, 0); if not Shift and (Hi(vk) > 0) then Keybd_Event(VK_SHIFT, $2A, KEYEVENTF_KEYUP, 0); end; end; end; procedure MakeWindowActive(wHandle: hWnd); begin if IsIconic(wHandle) then ShowWindow(wHandle, SW_RESTORE) else BringWindowToTop(wHandle); end; function GetHandleFromWindowTitle(TitleText: string): hWnd; var StrBuf: array[0..$FF] of Char; begin Result := FindWindow(PChar(0), StrPCopy(StrBuf, TitleText)); end;
procedure SendKeysToTitle(WindowTitle: string; Text: string); var Window: hWnd; begin Window := GetHandleFromWindowTitle(WindowTitle); MakeWindowActive(Window); SendKeyString(Text); end;
procedure SendKeysToHandle(WindowHandle: hWnd; Text: string); begin MakeWindowActive(WindowHandle); SendKeyString(Text); end;
end. |