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: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
|
procedure PostKeyEx32(key: Word; const shift: TShiftState; specialkey: Boolean); type TShiftKeyInfo = record shift: Byte; vkey: Byte; end; byteset = set of 0..7; const shiftkeys: array [1..3] of TShiftKeyInfo = ((shift: Ord(ssCtrl); vkey: VK_CONTROL), (shift: Ord(ssShift); vkey: VK_SHIFT), (shift: Ord(ssAlt); vkey: VK_MENU)); var flag: DWORD; bShift: ByteSet absolute shift; i: Integer; begin for i := 1 to 3 do begin if shiftkeys[i].shift in bShift then keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), 0, 0); end; if specialkey then flag := KEYEVENTF_EXTENDEDKEY else flag := 0;
keybd_event(key, MapvirtualKey(key, 0), flag, 0); flag := flag or KEYEVENTF_KEYUP; keybd_event(key, MapvirtualKey(key, 0), flag, 0);
for i := 3 downto 1 do begin if shiftkeys[i].shift in bShift then keybd_event(shiftkeys[i].vkey, MapVirtualKey(shiftkeys[i].vkey, 0), KEYEVENTF_KEYUP, 0); end; end;
procedure TForm1.Button1Click(Sender: TObject); begin PostKeyEx32(VK_LWIN, [], False);
PostKeyEx32(Ord('D'), [], False);
PostKeyEx32(Ord('C'), [ssctrl, ssAlt], False); end;
procedure TForm1.Button1Click(Sender: TObject); begin
Edit1.SetFocus; keybd_event(VK_SHIFT, 0, 0, 0); keybd_event(Ord('A'), 0, 0, 0); keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_LWIN, 0, 0, 0); keybd_event(Ord('R'), 0, 0, 0); keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); end;
procedure PostKeyExHWND(hWindow: HWnd; key: Word; const shift: TShiftState; specialkey: Boolean); type TBuffers = array [0..1] of TKeyboardState; var pKeyBuffers: ^TBuffers; lParam: LongInt; begin if IsWindow(hWindow) then begin pKeyBuffers := nil; lParam := MakeLong(0, MapVirtualKey(key, 0));
if specialkey then lParam := lParam or $1000000;
New(pKeyBuffers); try GetKeyboardState(pKeyBuffers^[1]); FillChar(pKeyBuffers^[0], SizeOf(TKeyboardState), 0);
if ssShift in shift then pKeyBuffers^[0][VK_SHIFT] := $80; if ssAlt in shift then begin pKeyBuffers^[0][VK_MENU] := $80; lParam := lParam or $20000000; end; if ssCtrl in shift then pKeyBuffers^[0][VK_CONTROL] := $80; if ssLeft in shift then pKeyBuffers^[0][VK_LBUTTON] := $80; if ssRight in shift then pKeyBuffers^[0][VK_RBUTTON] := $80; if ssMiddle in shift then pKeyBuffers^[0][VK_MBUTTON] := $80;
SetKeyboardState(pKeyBuffers^[0]); if ssAlt in Shift then begin PostMessage(hWindow, WM_SYSKEYDOWN, key, lParam); PostMessage(hWindow, WM_SYSKEYUP, key, lParam or $C0000000); end else begin PostMessage(hWindow, WM_KEYDOWN, key, lParam); PostMessage(hWindow, WM_KEYUP, key, lParam or $C0000000); end; Application.ProcessMessages;
SetKeyboardState(pKeyBuffers^[1]); finally if pKeyBuffers <> nil then Dispose(pKeyBuffers); end; end; end;
procedure TForm1.Button1Click(Sender: TObject); var targetWnd: HWND; begin targetWnd := FindWindow('notepad', nil) if targetWnd <> 0 then begin PostKeyExHWND(targetWnd, Ord('I'), [ssAlt], False); end; end;
procedure TForm1.Button1Click(Sender: TObject); const Str: string = 'writing writing writing'; var Inp: TInput; I: Integer; begin Edit1.SetFocus;
for I := 1 to Length(Str) do begin Inp.Itype := INPUT_KEYBOARD; Inp.ki.wVk := Ord(UpCase(Str[i])); Inp.ki.dwFlags := 0; SendInput(1, Inp, SizeOf(Inp));
Inp.Itype := INPUT_KEYBOARD; Inp.ki.wVk := Ord(UpCase(Str[i])); Inp.ki.dwFlags := KEYEVENTF_KEYUP; SendInput(1, Inp, SizeOf(Inp));
Application.ProcessMessages; Sleep(80); end; end;
procedure SendAltTab; var KeyInputs: array of TInput; KeyInputCount: Integer;
procedure KeybdInput(VKey: Byte; Flags: DWORD); begin Inc(KeyInputCount); SetLength(KeyInputs, KeyInputCount); KeyInputs[KeyInputCount - 1].Itype := INPUT_KEYBOARD; with KeyInputs[KeyInputCount - 1].ki do begin wVk := VKey; wScan := MapVirtualKey(wVk, 0); dwFlags := KEYEVENTF_EXTENDEDKEY; dwFlags := Flags or dwFlags; time := 0; dwExtraInfo := 0; end; end; begin KeybdInput(VK_MENU, 0); KeybdInput(VK_TAB, 0); KeybdInput(VK_TAB, KEYEVENTF_KEYUP); KeybdInput(VK_MENU, KEYEVENTF_KEYUP); SendInput(KeyInputCount, KeyInputs[0], SizeOf(KeyInputs[0])); end; |