首页  编辑  

模拟按键的另外一个方法

Tags: /超级猛料/Message.消息和事件/   Date Created:

模拟按键的另外一个方法:

下面的代码模拟按 Shift + <--

procedure TForm1.SpeedButton1Click(Sender: TObject);

var

 KeyState: TKeyboardState;

 OldCtrl: byte;

begin

 GetKeyboardState(KeyState);

 OldCtrl := KeyState[VK_SHIFT];

 KeyState[VK_SHIFT]:=$80;

 SetKeyboardState(KeyState);

 SendMessage (Memo1.Handle, WM_KEYDOWN, VK_LEFT, 0);

 SendMessage (Memo1.Handle, WM_KEYUP, VK_LEFT, 0);

 KeyState[VK_SHIFT]:=OldCtrl;

 SetKeyboardState(KeyState);

end;

---------------------------------------

procedure SendKey(H: Hwnd; Key: char);

var

 vKey, ScanCode: Word;

 lParam, ConvKey: Longint;

begin

 ConvKey := OemKeyScan(Ord(Key));

 ScanCode := ConvKey and $000000FF or $FF00;

 vKey := Ord(Key);

 lParam := LongInt(ScanCode) shl 16 or 1;

 SendMessage(H, WM_KEYDOWN, vKey, lParam);

 SendMessage(H, WM_CHAR, vKey, lParam);

 lParam := lParam or $C0000000;

 SendMessage(H, WM_KEYUP, vKey, lParam);

end;

SendKey(Richedit1.Handle, #8);