在Delphi中用什么方法可以实现模拟键盘输入,就象微软ABC输入法中的图形键盘一样。(100分)

  • 主题发起人 主题发起人 dreamdelphi
  • 开始时间 开始时间
D

dreamdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]在Delphi中用什么方法可以实现模拟键盘输入,就象微软ABC输入法中的图形键盘一样。
 
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0);//Key Up<br>keybd_event(VK_Control, 0, 0, 0 ); //Key Down
 
能否说得详细一些呢?谢了先!
 
抄了一段,你看看先。<br><br>让 WIN95 模拟按了一个按键,例如按下 ENTER或者 TAB 键?<br>PostMessage(Object.Handle, WM_KEYDOWN, VK_TAB, 0);<br><br><br><br>--------------------------------------------------------------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormKeyPress(Sender: TObject; var Key: Char);<br>&nbsp; private<br>&nbsp; &nbsp; AppInst: THandle;<br>&nbsp; &nbsp; AppWind: THandle;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses ShellAPI;<br>&nbsp;<br><br>procedure SendShift(H: HWnd; Down: Boolean);<br>var vKey, ScanCode, wParam: Word;<br>&nbsp; &nbsp; lParam: longint;<br>begin<br>&nbsp; vKey:= $10;<br>&nbsp; ScanCode:= MapVirtualKey(vKey, 0);<br>&nbsp; wParam:= vKey or ScanCode shl 8;<br>&nbsp; lParam:= longint(ScanCode) shl 16 or 1;<br>&nbsp; if not(Down) then lParam:= lParam or $C0000000;<br>&nbsp; SendMessage(H, WM_KEYDOWN, vKey, lParam);<br>end;<br><br>procedure SendCtrl(H: HWnd; Down: Boolean);<br>var vKey, ScanCode, wParam: Word;<br>&nbsp; &nbsp; lParam: longint;<br>begin<br>&nbsp; vKey:= $11;<br>&nbsp; ScanCode:= MapVirtualKey(vKey, 0);<br>&nbsp; wParam:= vKey or ScanCode shl 8;<br>&nbsp; lParam:= longint(ScanCode) shl 16 or 1;<br>&nbsp; if not(Down) then lParam:= lParam or $C0000000;<br>&nbsp; SendMessage(H, WM_KEYDOWN, vKey, lParam);<br>end;<br><br>procedure SendKey(H: Hwnd; Key: char);<br>var vKey, ScanCode, wParam: Word;<br>&nbsp; &nbsp; lParam, ConvKey: longint;<br>&nbsp; &nbsp; Shift, Ctrl: boolean;<br>begin<br>&nbsp; ConvKey:= OemKeyScan(ord(Key));<br>&nbsp; Shift:= (ConvKey and $00020000) &lt;&gt; 0;<br>&nbsp; Ctrl:= (ConvKey and $00040000) &lt;&gt; 0;<br>&nbsp; ScanCode:= ConvKey and $000000FF or $FF00;<br>&nbsp; vKey:= ord(Key);<br>&nbsp; wParam:= vKey;<br>&nbsp; lParam:= longint(ScanCode) shl 16 or 1;<br>&nbsp; if Shift then SendShift(H, true);<br>&nbsp; if Ctrl then SendCtrl(H, true);<br>&nbsp; SendMessage(H, WM_KEYDOWN, vKey, lParam);<br>&nbsp; SendMessage(H, WM_CHAR, vKey, lParam);<br>&nbsp; lParam:= lParam or $C0000000;<br>&nbsp; SendMessage(H, WM_KEYUP, vKey, lParam);<br>&nbsp; if Shift then SendShift(H, false);<br>&nbsp; if Ctrl then SendCtrl(H, false);<br>end;<br><br>function EnumFunc(Handle: HWnd; TF: TForm1): Bool; Far;<br>begin<br>&nbsp; TF.AppWind:= 0;<br>&nbsp; if GetWindowWord(Handle, GWW_HINSTANCE) = TF.AppInst then<br>&nbsp; &nbsp; TF.AppWind:= Handle;<br>&nbsp; result:= (TF.AppWind = 0);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var Text: Array[0..255] of char;<br>begin<br>&nbsp; AppInst:= ShellExecute(Handle, 'open', 'notepad.exe', nil, '', SW_NORMAL);<br>&nbsp; EnumWindows(@EnumFunc, longint(self));<br>&nbsp; AppWind:= GetWindow(AppWind, GW_CHILD);<br>end;<br><br>&nbsp;<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; SendKey(AppWind, 'T');<br>&nbsp; SendKey(AppWind, 'e');<br>&nbsp; SendKey(AppWind, 's');<br>&nbsp; SendKey(AppWind, 't');<br>end;<br><br>procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);<br>begin<br>&nbsp; if AppWind &lt;&gt; 0 then SendKey(AppWind, Key);<br>end;<br><br>end.<br><br><br><br>--------------------------------------------------------------------------------<br>Ah,这个嘛......正好在《DELPHI新闻组学习笔记》上看到一篇:<br>How to send [Alt]+[Down]?--------------发出一个Alt+Down 组合键<br><br>Re:<br>Use the keybd_event API function to fake keyboard events. Note that each<br>key down event needs a matching key up or you mess up the key state array.<br><br>&nbsp; keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), 0 , 0 ); &nbsp; &nbsp; // Alt down<br>&nbsp; keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), 0 , 0 ); &nbsp; &nbsp; // down arrow key down<br>&nbsp; keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN, 0 ), KEYEVENTF_KEYUP , 0 ); &nbsp;// down arrow key up<br>&nbsp; keybd_event( VK_MENU, MapVirtualKey( VK_MENU, 0 ), KEYEVENTF_KEYUP , 0 ); &nbsp;// Alt key up<br><br>经本人试验,确实可行,另加补充:<br>Ctrl: VK_CONTROL<br>SHIFT:VK_SHIFT<br>TAB: &nbsp;VK_TAB<br>'A': &nbsp;byte('A') &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(98-6-8 22:28)<br><br><br><br>--------------------------------------------------------------------------------<br>(*<br>Converts a string of characters and key names to keyboard events and<br>passes them to Windows.<br><br>Example syntax:<br><br>SendKeys('abc123{left}{left}{left}def{end}456{left 6}ghi{end}789', True);<br><br>*)<br><br>Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean;<br>type<br>&nbsp; WBytes = array[0..pred(SizeOf(Word))] of Byte;<br><br>&nbsp; TSendKey = record<br>&nbsp; &nbsp; Name : ShortString;<br>&nbsp; &nbsp; VKey : Byte;<br>&nbsp; end;<br><br>const<br>&nbsp; {Array of keys that SendKeys recognizes.<br><br>&nbsp; If you add to this list, you must be sure to keep it sorted alphabetically<br>&nbsp; by Name because a binary search routine is used to scan it.}<br><br>&nbsp; MaxSendKeyRecs = 41;<br>&nbsp; SendKeyRecs : array[1..MaxSendKeyRecs] of TSendKey =<br>&nbsp; (<br>&nbsp; &nbsp;(Name:'BKSP'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_BACK),<br>&nbsp; &nbsp;(Name:'BS'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_BACK),<br>&nbsp; &nbsp;(Name:'BACKSPACE'; &nbsp; &nbsp; &nbsp; VKey:VK_BACK),<br>&nbsp; &nbsp;(Name:'BREAK'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_CANCEL),<br>&nbsp; &nbsp;(Name:'CAPSLOCK'; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_CAPITAL),<br>&nbsp; &nbsp;(Name:'CLEAR'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_CLEAR),<br>&nbsp; &nbsp;(Name:'DEL'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_DELETE),<br>&nbsp; &nbsp;(Name:'DELETE'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_DELETE),<br>&nbsp; &nbsp;(Name:'DOWN'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_DOWN),<br>&nbsp; &nbsp;(Name:'END'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_END),<br>&nbsp; &nbsp;(Name:'ENTER'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_RETURN),<br>&nbsp; &nbsp;(Name:'ESC'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_ESCAPE),<br>&nbsp; &nbsp;(Name:'ESCAPE'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_ESCAPE),<br>&nbsp; &nbsp;(Name:'F1'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F1),<br>&nbsp; &nbsp;(Name:'F10'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F10),<br>&nbsp; &nbsp;(Name:'F11'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F11),<br>&nbsp; &nbsp;(Name:'F12'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F12),<br>&nbsp; &nbsp;(Name:'F13'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F13),<br>&nbsp; &nbsp;(Name:'F14'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F14),<br>&nbsp; &nbsp;(Name:'F15'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F15),<br>&nbsp; &nbsp;(Name:'F16'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_F16),<br>&nbsp; &nbsp;(Name:'F2'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F2),<br>&nbsp; &nbsp;(Name:'F3'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F3),<br>&nbsp; &nbsp;(Name:'F4'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F4),<br>&nbsp; &nbsp;(Name:'F5'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F5),<br>&nbsp; &nbsp;(Name:'F6'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F6),<br>&nbsp; &nbsp;(Name:'F7'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F7),<br>&nbsp; &nbsp;(Name:'F8'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F8),<br>&nbsp; &nbsp;(Name:'F9'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_F9),<br>&nbsp; &nbsp;(Name:'HELP'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_HELP),<br>&nbsp; &nbsp;(Name:'HOME'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_HOME),<br>&nbsp; &nbsp;(Name:'INS'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_INSERT),<br>&nbsp; &nbsp;(Name:'LEFT'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_LEFT),<br>&nbsp; &nbsp;(Name:'NUMLOCK'; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_NUMLOCK),<br>&nbsp; &nbsp;(Name:'PGDN'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_NEXT),<br>&nbsp; &nbsp;(Name:'PGUP'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_PRIOR),<br>&nbsp; &nbsp;(Name:'PRTSC'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_PRINT),<br>&nbsp; &nbsp;(Name:'RIGHT'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_RIGHT),<br>&nbsp; &nbsp;(Name:'SCROLLLOCK'; &nbsp; &nbsp; &nbsp;VKey:VK_SCROLL),<br>&nbsp; &nbsp;(Name:'TAB'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_TAB),<br>&nbsp; &nbsp;(Name:'UP'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_UP)<br>&nbsp; );<br><br>&nbsp; {Extra VK constants missing from Delphi's Windows API interface}<br>&nbsp; VK_NULL=0;<br>&nbsp; VK_SemiColon=186;<br>&nbsp; VK_Equal=187;<br>&nbsp; VK_Comma=188;<br>&nbsp; VK_Minus=189;<br>&nbsp; VK_Period=190;<br>&nbsp; VK_Slash=191;<br>&nbsp; VK_BackQuote=192;<br>&nbsp; VK_LeftBracket=219;<br>&nbsp; VK_BackSlash=220;<br>&nbsp; VK_RightBracket=221;<br>&nbsp; VK_Quote=222;<br>&nbsp; VK_Last=VK_Quote;<br><br>&nbsp; ExtendedVKeys : set of byte =<br>&nbsp; [VK_Up,<br>&nbsp; &nbsp;VK_Down,<br>&nbsp; &nbsp;VK_Left,<br>&nbsp; &nbsp;VK_Right,<br>&nbsp; &nbsp;VK_Home,<br>&nbsp; &nbsp;VK_End,<br>&nbsp; &nbsp;VK_Prior, &nbsp;{PgUp}<br>&nbsp; &nbsp;VK_Next, &nbsp; {PgDn}<br>&nbsp; &nbsp;VK_Insert,<br>&nbsp; &nbsp;VK_Delete];<br><br>const<br>&nbsp; INVALIDKEY = $FFFF {Unsigned -1};<br>&nbsp; VKKEYSCANSHIFTON = $01;<br>&nbsp; VKKEYSCANCTRLON = $02;<br>&nbsp; VKKEYSCANALTON = $04;<br>&nbsp; UNITNAME = 'SendKeys';<br>var<br>&nbsp; UsingParens, ShiftDown, ControlDown, AltDown, FoundClose : Boolean;<br>&nbsp; PosSpace : Byte;<br>&nbsp; I, L : Integer;<br>&nbsp; NumTimes, MKey : Word;<br>&nbsp; KeyString : String[20];<br><br>procedure DisplayMessage(Message : PChar);<br>begin<br>&nbsp; MessageBox(0,Message,UNITNAME,0);<br>end;<br><br>function BitSet(BitTable, BitMask : Byte) : Boolean;<br>begin<br>&nbsp; Result:=ByteBool(BitTable and BitMask);<br>end;<br><br>procedure SetBit(var BitTable : Byte; BitMask : Byte);<br>begin<br>&nbsp; BitTable:=BitTable or Bitmask;<br>end;<br><br>Procedure KeyboardEvent(VKey, ScanCode : Byte; Flags : Longint);<br>var<br>&nbsp; KeyboardMsg : TMsg;<br>begin<br>&nbsp; keybd_event(VKey, ScanCode, Flags,0);<br>&nbsp; If (Wait) then While (PeekMessage(KeyboardMsg,0,WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)) do begin<br>&nbsp; &nbsp; TranslateMessage(KeyboardMsg);<br>&nbsp; &nbsp; DispatchMessage(KeyboardMsg);<br>&nbsp; end;<br>end;<br><br>Procedure SendKeyDown(VKey: Byte; NumTimes : Word; GenUpMsg : Boolean);<br>var<br>&nbsp; Cnt : Word;<br>&nbsp; ScanCode : Byte;<br>&nbsp; NumState : Boolean;<br>&nbsp; KeyBoardState : TKeyboardState;<br>begin<br>&nbsp; If (VKey=VK_NUMLOCK) then begin<br>&nbsp; &nbsp; NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1);<br>&nbsp; &nbsp; GetKeyBoardState(KeyBoardState);<br>&nbsp; &nbsp; If NumState then KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1)<br>&nbsp; &nbsp; else KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1);<br>&nbsp; &nbsp; SetKeyBoardState(KeyBoardState);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; ScanCode:=Lo(MapVirtualKey(VKey,0));<br>&nbsp; For Cnt:=1 to NumTimes do<br>&nbsp; &nbsp; If (VKey in ExtendedVKeys)then begin<br>&nbsp; &nbsp; &nbsp; KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);<br>&nbsp; &nbsp; &nbsp; If (GenUpMsg) then<br>&nbsp; &nbsp; &nbsp; &nbsp; KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)<br>&nbsp; &nbsp; end else begin<br>&nbsp; &nbsp; &nbsp; KeyboardEvent(VKey, ScanCode, 0);<br>&nbsp; &nbsp; &nbsp; If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);<br>&nbsp; &nbsp; end;<br>end;<br><br>Procedure SendKeyUp(VKey: Byte);<br>var<br>&nbsp; ScanCode : Byte;<br>begin<br>&nbsp; ScanCode:=Lo(MapVirtualKey(VKey,0));<br>&nbsp; If (VKey in ExtendedVKeys)then<br>&nbsp; &nbsp; KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)<br>&nbsp; else KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);<br>end;<br><br>Procedure SendKey(MKey: Word; NumTimes : Word; GenDownMsg : Boolean);<br>begin<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyDown(VK_SHIFT,1,False);<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyDown(VK_CONTROL,1,False);<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyDown(VK_MENU,1,False);<br>&nbsp; SendKeyDown(Lo(MKey), NumTimes, GenDownMsg);<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyUp(VK_SHIFT);<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyUp(VK_CONTROL);<br>&nbsp; If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyUp(VK_MENU);<br>end;<br><br>{Implements a simple binary search to locate special key name strings}<br><br>Function StringToVKey(KeyString : ShortString) : Word;<br>var<br>&nbsp; Found, Collided : Boolean;<br>&nbsp; Bottom, Top, Middle : Byte;<br>begin<br>&nbsp; Result:=INVALIDKEY;<br>&nbsp; Bottom:=1;<br>&nbsp; Top:=MaxSendKeyRecs;<br>&nbsp; Found:=false;<br>&nbsp; Middle:=(Bottom+Top) div 2;<br>&nbsp; Repeat<br>&nbsp; &nbsp; Collided:=((Bottom=Middle) or (Top=Middle));<br>&nbsp; &nbsp; If (KeyString=SendKeyRecs[Middle].Name) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Found:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp;Result:=SendKeyRecs[Middle].VKey;<br>&nbsp; &nbsp; end else begin<br>&nbsp; &nbsp; &nbsp; &nbsp;If (KeyString&gt;SendKeyRecs[Middle].Name) then Bottom:=Middle<br>&nbsp; &nbsp; &nbsp; &nbsp;else Top:=Middle;<br>&nbsp; &nbsp; &nbsp; &nbsp;Middle:=(Succ(Bottom+Top)) div 2;<br>&nbsp; &nbsp; end;<br>&nbsp; Until (Found or Collided);<br>&nbsp; If (Result=INVALIDKEY) then DisplayMessage('Invalid Key Name');<br>end;<br><br>procedure PopUpShiftKeys;<br>begin<br>&nbsp; If (not UsingParens) then begin<br>&nbsp; &nbsp; If ShiftDown then SendKeyUp(VK_SHIFT);<br>&nbsp; &nbsp; If ControlDown then SendKeyUp(VK_CONTROL);<br>&nbsp; &nbsp; If AltDown then SendKeyUp(VK_MENU);<br>&nbsp; &nbsp; ShiftDown:=false;<br>&nbsp; &nbsp; ControlDown:=false;<br>&nbsp; &nbsp; AltDown:=false;<br>&nbsp; end;<br>end;<br><br>begin<br>&nbsp; AllocationSize:=MaxInt;<br>&nbsp; Result:=false;<br>&nbsp; UsingParens:=false;<br>&nbsp; ShiftDown:=false;<br>&nbsp; ControlDown:=false;<br>&nbsp; AltDown:=false;<br>&nbsp; I:=0;<br>&nbsp; L:=StrLen(SendKeysString);<br>&nbsp; If (L&gt;AllocationSize) then L:=AllocationSize;<br>&nbsp; If (L=0) then Exit;<br><br>&nbsp; While (I&lt;L) do begin<br>&nbsp; &nbsp; case SendKeysString of<br>&nbsp; &nbsp; '(' : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsingParens:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; ')' : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UsingParens:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; '%' : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AltDown:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendKeyDown(VK_MENU,1,False);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; '+' : &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShiftDown:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendKeyDown(VK_SHIFT,1,False);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; '^' : &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ControlDown:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendKeyDown(VK_CONTROL,1,False);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; '{' : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NumTimes:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (SendKeysString[Succ(I)]='{') then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MKey:=VK_LEFTBRACKET;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetBit(Wbytes(MKey)[1],VKKEYSCANSHIFTON);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKey(MKey,1,True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I,3);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyString:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FoundClose:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; While (I&lt;=L) do begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (SendKeysString='}') then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FoundClose:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyString:=KeyString+Upcase(SendKeysString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (Not FoundClose) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DisplayMessage('No Close');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (SendKeysString='}') then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MKey:=VK_RIGHTBRACKET;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetBit(Wbytes(MKey)[1],VKKEYSCANSHIFTON);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKey(MKey,1,True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PosSpace:=Pos(' ',KeyString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (PosSpace&lt;&gt;0) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NumTimes:=StrToInt(Copy(KeyString,Succ(PosSpace),Length(KeyString)-PosSpace));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;KeyString:=Copy(KeyString,1,Pred(PosSpace));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (Length(KeyString)=1) then MKey:=vkKeyScan(KeyString[1])<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else MKey:=StringToVKey(KeyString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If (MKey&lt;&gt;INVALIDKEY) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKey(MKey,NumTimes,True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; '~' : begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendKeyDown(VK_RETURN,1,True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; else &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MKey:=vkKeyScan(SendKeysString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If (MKey&lt;&gt;INVALIDKEY) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendKey(MKey,1,True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PopUpShiftKeys;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end else DisplayMessage('Invalid KeyName');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Result:=true;<br>&nbsp; PopUpShiftKeys;<br>end;<br><br><br><br>--------------------------------------------------------------------------------<br><br><br>问题提出/摘要:<br><br>当使用Enter键完成Tab键功能时,如何避免Windows产生的Beep声音? <br><br>&nbsp;<br><br>回答:<br><br>&nbsp; 新代码的目标是当在TEdit等控件中按Enter键时避免Windows产生的Beep声音。<br><br>&nbsp; <br><br>&nbsp; 注意:如果你是Delphi初学者,下面的第一个过程必须手工地在form类中正确声明才能使用。<br><br>&nbsp; <br><br>&nbsp; procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char); <br><br>&nbsp; begin <br><br>&nbsp; if key = #13 then key := #0; <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; <br><br>&nbsp; Shift: TShiftState); <br><br>&nbsp; var <br><br>&nbsp; ACtrl: TWinControl; <br><br>&nbsp; k: TKeyPressEvent; <br><br>&nbsp; begin <br><br>&nbsp; <br><br>&nbsp; if key = 13 then <br><br>&nbsp; begin <br><br>&nbsp; ACtrl := ActiveControl; <br><br>&nbsp; if ACtrl is TCustomMemo then exit; <br><br>&nbsp; <br><br>&nbsp; if ACtrl is TEdit then <br><br>&nbsp; begin <br><br>&nbsp; if assigned(TEdit(ACtrl).onKeyPress) then <br><br>&nbsp; k:= TEdit(ACtrl).OnKeyPress; <br><br>&nbsp; TEdit(ACtrl).OnKeyPress := EditKeyPress; <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; repeat <br><br>&nbsp; ACtrl:= FindNextControl(ACtrl,true,true,false); <br><br>&nbsp; until (ACtrl is TCustomEdit) or <br><br>&nbsp; (ACtrl is TCustomComboBox) or <br><br>&nbsp; (ACtrl is TCustomListBox) or <br><br>&nbsp; (ACtrl is TCustomCheckBox) or <br><br>&nbsp; (ACtrl is TRadioButton); <br><br>&nbsp; <br><br>&nbsp; if ACtrl is TEdit then <br><br>&nbsp; begin <br><br>&nbsp; if assigned(K) then <br><br>&nbsp; TEdit(ACtrl).OnKeyPress := K; <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; ACtrl.SetFocus ; <br><br>&nbsp; end; <br><br>&nbsp; <br><br>&nbsp; end; <br><br>&nbsp;<br><br>&nbsp;<br><br>&nbsp; <br>
 
函数功能:该函数合成一次击键事件。系统可使用这种合成的击键事件来产生WM_KEYUP或<br>WM_KEYDOWN消息,键盘驱动程序的中断处理程序调用keybd_event函数。在Windows NT中<br>该函数己被使用Sendlhput来替代它。<br><br>&nbsp; &nbsp; 函数原型;VOID keybd_event(BYTE bVk,BYTE bScan,DWORD dwFlags,DWORD <br>&nbsp; &nbsp; dwExtralnfo);<br><br>&nbsp; &nbsp; 参数:<br><br>&nbsp; &nbsp; bVk:定义一个虚据拟键码。键码值必须在1~254之间。<br><br>&nbsp; &nbsp; bScan:定义该键的硬件扫描码。<br><br>&nbsp; &nbsp; dwFlags:定义函数操作的名个方面的一个标志位集。应用程序可使用如下一些预定义<br>常数的组合设置标志位。<br><br>&nbsp; &nbsp; KEYEVENTF_EXETENDEDKEY:若指定该值,则扫描码前一个值为OXEO(224)的前缀<br>字节。DEYEVENTF_KEYUP:若指定该值,该键将被释放;若未指定该值,该键交被接下。<br>dwExtralnfo:定义与击键相关的附加的32位值。<br><br>&nbsp; &nbsp; 返回值:该函数无返回值。<br><br>&nbsp; &nbsp; 备注:尽管keybd_event传递一个与OEM相关的硬件扫描码给系统,但应用程序不能用<br>此扫描码。系统在内部将扫描码转换成虚拟键码,并且在传送给应用程序前清除键码的<br>UP/down位。应用程序可以摸拟PRINTSCREEN键的按下来获得一个屏幕快照,并把它存放到<br>剪切板中。若要做到这一点,则要将keybd_event的bVk参数置为VK_SNAPSHOT,bScan参数置<br>为0(用以获得全屏快照)或hScan置为1(仅获得活动窗口的快照)。<br>Windows CE:WindowsCE支持dwFlags参数附加的标志位。即使用KEYEVENTF_SILENT标志<br>模拟击键,而不产生敲击的声音。Windows CE不支持KEYEVENTF_EXTENDEDKEY标志。<br>
 
haha,在Delphi安装盘的Info目录里有一个SendKeys,里面有相关的东西,好用噢
 
谢谢各位!
 
多人接受答案了。
 

Similar threads

后退
顶部