请教如何使用:keybd_event?(50)

  • 主题发起人 主题发起人 tinyliu2009
  • 开始时间 开始时间
T

tinyliu2009

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);var C:BYTE;begin memo1.SetFocus; C:=BYTE('a'); keybd_event( C, mapvirtualkey(C,0),0,0); keybd_event( C, mapvirtualkey(C,0),KEYEVENTF_KEYUP,0);end;执行后MEMO1显示的不是'a',而是'1'。如果将'a'改为'A',则memo1中显示的是'A',如果将'a'改为'.',在MEMO1中反应的是Delete键的动作,会删除光标后面的字符。所以此函数用得非常迷惑,请高手赐教。我要模拟键盘输入的是字符串'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.'等可见字符。
 
下面三个测试都是一样的,给你参考一下。procedure TForm1.Button1Click(Sender: TObject);type vkalphabet = (a = $41, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z); vknumeral = (vk0 = $30, vk1, vk2, vk3, vk4, vk5, vk6, vk7, vk8, vk9);var alphabet: vkalphabet; umeral: vknumeral;begin Memo1.SetFocus; for alphabet := a to z do begin keybd_event(integer(alphabet), MapVirtualKey(integer(alphabet), 0), 0, 0); keybd_event(integer(alphabet), MapVirtualKey(integer(alphabet), 0), KEYEVENTF_KEYUP, 0); end; for umeral := vk0 to vk9 do begin keybd_event(integer(umeral), MapVirtualKey(integer(umeral), 0), 0, 0); keybd_event(integer(umeral), MapVirtualKey(integer(umeral), 0), KEYEVENTF_KEYUP, 0); end;end;procedure TForm1.Button2Click(Sender: TObject);var c: Char;begin Memo1.SetFocus; for c := '0' to '9' do begin keybd_event(Ord(c), MapVirtualKey(Ord(c), 0), 0, 0); keybd_event(Ord(c), MapVirtualKey(Ord(c), 0), KEYEVENTF_KEYUP, 0); end; for c := 'A' to 'Z' do begin keybd_event(Ord(c), MapVirtualKey(Ord(c), 0), 0, 0); keybd_event(Ord(c), MapVirtualKey(Ord(c), 0), KEYEVENTF_KEYUP, 0); end;end;procedure TForm1.Button3Click(Sender: TObject);var c: integer;begin Memo1.SetFocus; for c := $41 to $41 + 25 do begin keybd_event(c, MapVirtualKey(c, 0), 0, 0); keybd_event(c, MapVirtualKey(c, 0), KEYEVENTF_KEYUP, 0); end; keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0); //shift键按下 for c := $41 to $41 + 25 do begin keybd_event(c, MapVirtualKey(c, 0), 0, 0); keybd_event(c, MapVirtualKey(c, 0), KEYEVENTF_KEYUP, 0); end; keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0); //shift键弹起 for c := $30 to $30 + 9 do begin keybd_event(c, MapVirtualKey(c, 0), 0, 0); keybd_event(c, MapVirtualKey(c, 0), KEYEVENTF_KEYUP, 0); end;end;
 
后退
顶部