下面是一个模拟按键的单元,你可以分析一下!<br>主要部分是Keybd_Event函数的使用!<br>{****************************************************}<br>{ SendKeys Unit for Delphi 32 }<br>{ Copyright (c) 1999 by Borut Batagelj (Slovenia) }<br>{ Aleksey Kuznetsov (Ukraine) }<br>{ Home Page: www.utilmind.com }<br>{ E-Mail: info@utilmind.com }<br>{****************************************************}<br><br>unit SendKeys;<br><br>interface<br><br>uses<br> Windows, SysUtils;<br><br>const<br> SK_BKSP = #8;<br> SK_TAB = #9;<br> SK_ENTER = #13;<br> SK_ESC = #27;<br> SK_ADD = #107;<br> SK_SUB = #109;<br> SK_F1 = #228;<br> SK_F2 = #229;<br> SK_F3 = #230;<br> SK_F4 = #231;<br> SK_F5 = #232;<br> SK_F6 = #233;<br> SK_F7 = #234;<br> SK_F8 = #235;<br> SK_F9 = #236;<br> SK_F10 = #237;<br> SK_F11 = #238;<br> SK_F12 = #239;<br> SK_HOME = #240;<br> SK_END = #241;<br> SK_UP = #242;<br> SK_DOWN = #243;<br> SK_LEFT = #244;<br> SK_RIGHT = #245;<br> SK_PGUP = #246;<br> SK_PGDN = #247;<br> SK_INS = #248;<br> SK_DEL = #249;<br> SK_SHIFT_DN = #250;<br> SK_SHIFT_UP = #251;<br> SK_CTRL_DN = #252;<br> SK_CTRL_UP = #253;<br> SK_ALT_DN = #254;<br> SK_ALT_UP = #255;<br><br>procedure SendKeyString(Text: String);<br><br>implementation<br><br>procedure SendKeyString(Text: String);<br>var<br> i: Integer;<br> Shift: Boolean;<br> vk, ScanCode: Word;<br> ch: Char;<br> c, s: Byte;<br>const<br> vk_keys: Array[0..9] of Byte =<br> (VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT,<br> VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE);<br> vk_shft: Array[0..2] of Byte = (VK_SHIFT, VK_CONTROL, VK_MENU);<br> flags: Array[False..True] of Integer = (KEYEVENTF_KEYUP, 0);<br>begin<br> Shift := False;<br> for i := 1 to Length(Text) do<br> begin<br> ch := Text;<br> if ch >= #250 then<br> begin<br> s := Ord(ch) - 250;<br> Shift := not Odd(s);<br> c := vk_shft[s shr 1];<br> ScanCode := MapVirtualKey(c,0);<br> Keybd_Event(c, Scancode, Flags[shift], 0);<br> end<br> else<br> begin<br> vk := 0;<br> if ch >= #240 then<br> c := vk_keys[Ord(ch) - 240]<br> else<br> if ch >= #228 then {228 (F1) => $70 (vk_F1)}<br> c := Ord(ch) - 116<br> else<br> if ch < #110 then<br> c := Ord(ch)<br> else<br> begin<br> vk := VkKeyScan(ch);<br> c := LoByte(vk);<br> end;<br> ScanCode := MapVirtualKey(c,0);<br> if not Shift and (Hi(vk) > 0) then { $2A = scancode of VK_SHIFT }<br> Keybd_Event(VK_SHIFT, $2A, 0, 0);<br> Keybd_Event(c,scancode, 0, 0);<br> Keybd_Event(c,scancode, KEYEVENTF_KEYUP, 0);<br> if not Shift and (Hi(vk) > 0) then<br> Keybd_Event(VK_SHIFT, $2A, KEYEVENTF_KEYUP, 0);<br> end;<br> end;<br>end;<br>end. <br>