怎么样对 Handle 发送组合键,ctrl+enter(300分)

  • 主题发起人 主题发起人 人在昆明
  • 开始时间 开始时间

人在昆明

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么样对 Handle 发送组合键,ctrl+enter
 
Delphi 光盘中有一个 SendKey 单元(好像是叫SNDKEY32.PAS),使用那里面的SendKeys函数就可以完成你的要求,在这个单元里,有如何发送组合键的说明
 
?可有例子,单元我看过不知道怎么发送!
 
那个单元就两个函数,一个函数是用来将指定窗口名的窗体激活(变成活动窗口),<br>另一个函数就是 SendKeys<br>声明如下<br>Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean;<br>function AppActivate(WindowName : PChar) : boolean;<br>使用方法是:<br>AppActivate(PChar('窗体标题名'));<br>SendKeys(PChar('^~'), False);//发送ctrl+enter<br>
 
下面是 DELPHI7 附带的 SendKey32.PAS 单元<br>其中有说明也有例子<br><br>(*<br>SendKeys routine for 32-bit Delphi.<br><br>Written by Ken Henderson<br><br>Copyright (c) 1995 Ken Henderson<br><br>This unit includes two routines that simulate popular Visual Basic<br>routines: Sendkeys and AppActivate. &nbsp;SendKeys takes a PChar<br>as its first parameter and a boolean as its second, like so:<br><br>SendKeys('KeyString', Wait);<br><br>where KeyString is a string of key names and modifiers that you want<br>to send to the current input focus and Wait is a boolean variable or value<br>that indicates whether SendKeys should wait for each key message to be<br>processed before proceeding. &nbsp;See the table below for more information.<br><br>AppActivate also takes a PChar as its only parameter, like so:<br><br>AppActivate('WindowName');<br><br>where WindowName is the name of the window that you want to make the<br>current input focus.<br><br>SendKeys supports the Visual Basic SendKeys syntax, as documented below.<br><br>Supported modifiers:<br><br>+ = Shift<br>^ = Control<br>% = Alt<br><br>Surround sequences of characters or key names with parentheses in order to<br>modify them as a group. &nbsp;For example, '+abc' shifts only 'a', while '+(abc)' shifts<br>all three characters.<br><br>Supported special characters<br><br>~ = Enter<br>( = Begin modifier group (see above)<br>) = End modifier group (see above)<br>{ = Begin key name text (see below)<br>} = End key name text (see below)<br><br>Supported characters:<br><br>Any character that can be typed is supported. &nbsp;Surround the modifier keys<br>listed above with braces in order to send as normal text.<br><br>Supported key names (surround these with braces):<br><br>BKSP, BS, BACKSPACE<br>BREAK<br>CAPSLOCK<br>CLEAR<br>DEL<br>DELETE<br>DOWN<br>END<br>ENTER<br>ESC<br>ESCAPE<br>F1<br>F2<br>F3<br>F4<br>F5<br>F6<br>F7<br>F8<br>F9<br>F10<br>F11<br>F12<br>F13<br>F14<br>F15<br>F16<br>HELP<br>HOME<br>INS<br>LEFT<br>NUMLOCK<br>PGDN<br>PGUP<br>PRTSC<br>RIGHT<br>SCROLLLOCK<br>TAB<br>UP<br><br>Follow the keyname with a space and a number to send the specified key a<br>given number of times (e.g., {left 6}).<br>*)<br><br>unit sndkey32;<br><br>interface<br><br>Uses SysUtils, Windows, Messages;<br><br>Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean;<br>function AppActivate(WindowName : PChar) : boolean;<br><br>{Buffer for working with PChar's}<br><br>const<br>&nbsp; WorkBufLen = 40;<br>var<br>&nbsp; WorkBuf : array[0..WorkBufLen] of Char;<br><br>implementation<br>type<br>&nbsp; THKeys = array[0..pred(MaxLongInt)] of byte;<br>var<br>&nbsp; AllocationSize : integer;<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:'BACKSPACE'; &nbsp; &nbsp; &nbsp; VKey:VK_BACK),<br>&nbsp; &nbsp;(Name:'BKSP'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_BACK),<br>&nbsp; &nbsp;(Name:'BREAK'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VKey:VK_CANCEL),<br>&nbsp; &nbsp;(Name:'BS'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VKey:VK_BACK),<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>{AppActivate<br><br>This is used to set the current input focus to a given window using its<br>name. &nbsp;This is especially useful for ensuring a window is active before<br>sending it input messages using the SendKeys function. &nbsp;You can specify<br>a window's name in its entirety, or only portion of it, beginning from<br>the left.<br><br>}<br><br><br>var<br>&nbsp; WindowHandle : HWND;<br><br>function EnumWindowsProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall;<br>var<br>&nbsp; WindowName : array[0..MAX_PATH] of char;<br>begin<br>&nbsp; {Can't test GetWindowText's return value since some windows don't have a title}<br>&nbsp; GetWindowText(WHandle,WindowName,MAX_PATH);<br>&nbsp; Result := (StrLIComp(WindowName,PChar(lParam), StrLen(PChar(lParam))) &lt;&gt; 0);<br>&nbsp; If (not Result) then WindowHandle:=WHandle;<br>end;<br><br>function AppActivate(WindowName : PChar) : boolean;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Result:=true;<br>&nbsp; &nbsp; WindowHandle:=FindWindow(nil,WindowName);<br>&nbsp; &nbsp; If (WindowHandle=0) then EnumWindows(@EnumWindowsProc,Integer(PChar(WindowName)));<br>&nbsp; &nbsp; If (WindowHandle&lt;&gt;0) then begin<br>&nbsp; &nbsp; &nbsp; SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);<br>&nbsp; &nbsp; &nbsp; SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow(WindowHandle);<br>&nbsp; &nbsp; end else Result:=false;<br>&nbsp; except<br>&nbsp; &nbsp; on Exception do Result:=false;<br>&nbsp; end;<br>end;<br><br>end.<br>
 
这个不行,这个还要切换焦点,不知道怎么发消息?
 
转贴:<br><br>下面是一个模拟按键的单元,你可以分析一下!<br>主要部分是Keybd_Event函数的使用!<br>{****************************************************}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendKeys Unit for Delphi 32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp;Copyright (c) 1999 by Borut Batagelj (Slovenia) }<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Aleksey Kuznetsov (Ukraine) &nbsp;}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Home Page: www.utilmind.com &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;E-Mail: info@utilmind.com &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{****************************************************}<br><br>unit SendKeys;<br><br>interface<br><br>uses<br>&nbsp; Windows, SysUtils;<br><br>const<br>&nbsp; SK_BKSP = #8;<br>&nbsp; SK_TAB = #9;<br>&nbsp; SK_ENTER = #13;<br>&nbsp; SK_ESC = #27;<br>&nbsp; SK_ADD = #107;<br>&nbsp; SK_SUB = #109;<br>&nbsp; SK_F1 = #228;<br>&nbsp; SK_F2 = #229;<br>&nbsp; SK_F3 = #230;<br>&nbsp; SK_F4 = #231;<br>&nbsp; SK_F5 = #232;<br>&nbsp; SK_F6 = #233;<br>&nbsp; SK_F7 = #234;<br>&nbsp; SK_F8 = #235;<br>&nbsp; SK_F9 = #236;<br>&nbsp; SK_F10 = #237;<br>&nbsp; SK_F11 = #238;<br>&nbsp; SK_F12 = #239;<br>&nbsp; SK_HOME = #240;<br>&nbsp; SK_END = #241;<br>&nbsp; SK_UP = #242;<br>&nbsp; SK_DOWN = #243;<br>&nbsp; SK_LEFT = #244;<br>&nbsp; SK_RIGHT = #245;<br>&nbsp; SK_PGUP = #246;<br>&nbsp; SK_PGDN = #247;<br>&nbsp; SK_INS = #248;<br>&nbsp; SK_DEL = #249;<br>&nbsp; SK_SHIFT_DN = #250;<br>&nbsp; SK_SHIFT_UP = #251;<br>&nbsp; SK_CTRL_DN = #252;<br>&nbsp; SK_CTRL_UP = #253;<br>&nbsp; SK_ALT_DN = #254;<br>&nbsp; SK_ALT_UP = #255;<br><br>procedure SendKeyString(Text: String);<br><br>implementation<br><br>procedure SendKeyString(Text: String);<br>var<br>&nbsp; &nbsp;i: Integer;<br>&nbsp; &nbsp;Shift: Boolean;<br>&nbsp; &nbsp;vk, ScanCode: Word;<br>&nbsp; &nbsp;ch: Char;<br>&nbsp; &nbsp;c, s: Byte;<br>const<br>&nbsp; &nbsp;vk_keys: Array[0..9] of Byte =<br>&nbsp; &nbsp; &nbsp; (VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT,<br>&nbsp; &nbsp; &nbsp; &nbsp;VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE);<br>&nbsp; &nbsp;vk_shft: Array[0..2] of Byte = (VK_SHIFT, VK_CONTROL, VK_MENU);<br>&nbsp; &nbsp;flags: Array[False..True] of Integer = (KEYEVENTF_KEYUP, 0);<br>begin<br>&nbsp; &nbsp;Shift := False;<br>&nbsp; &nbsp;for i := 1 to Length(Text) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;ch := Text;<br>&nbsp; &nbsp; &nbsp;if ch &gt;= #250 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;s := Ord(ch) - 250;<br>&nbsp; &nbsp; &nbsp; &nbsp;Shift := not Odd(s);<br>&nbsp; &nbsp; &nbsp; &nbsp;c := vk_shft[s shr 1];<br>&nbsp; &nbsp; &nbsp; &nbsp;ScanCode := MapVirtualKey(c,0);<br>&nbsp; &nbsp; &nbsp; &nbsp;Keybd_Event(c, Scancode, Flags[shift], 0);<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;vk := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;if ch &gt;= #240 then<br>&nbsp; &nbsp; &nbsp; &nbsp; c := vk_keys[Ord(ch) - 240]<br>&nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; if ch &gt;= #228 then {228 (F1) =&gt; $70 (vk_F1)}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c := Ord(ch) - 116<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ch &lt; #110 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c := Ord(ch)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vk := VkKeyScan(ch);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c := LoByte(vk);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp;ScanCode := MapVirtualKey(c,0);<br>&nbsp; &nbsp; &nbsp; &nbsp;if not Shift and (Hi(vk) &gt; 0) then { $2A = scancode of VK_SHIFT }<br>&nbsp; &nbsp; &nbsp; &nbsp; Keybd_Event(VK_SHIFT, $2A, 0, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp;Keybd_Event(c,scancode, 0, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp;Keybd_Event(c,scancode, KEYEVENTF_KEYUP, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp;if not Shift and (Hi(vk) &gt; 0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; Keybd_Event(VK_SHIFT, $2A, KEYEVENTF_KEYUP, 0);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;<br>end. <br>
 
用keybd_event<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>&nbsp; &nbsp;Ctrl: VK_CONTROL<br>&nbsp; &nbsp;SHIFT:VK_SHIFT<br>&nbsp; &nbsp;TAB: &nbsp;VK_TAB<br>&nbsp; &nbsp;'A': &nbsp;byte('A') &nbsp;<br><br>
 
楼上的 也是要切换焦点!
 
可不可以得到焦点后再让它失去焦点?
 
我自己解决啦!
 
后退
顶部