首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

所有家当求BCB模拟键盘鼠标操作,该如何处理

2012-02-07 
所有家当求BCB模拟键盘鼠标操作还是模拟键盘A/F1/1/2/3按下以及鼠标点击的方法,已经可以确定,程序接受Send

所有家当求BCB模拟键盘鼠标操作
还是模拟键盘A/F1/1/2/3按下以及鼠标点击的方法,已经可以确定,程序接受SendMessage/PostMessage的模拟操作(在VB中用SendMessage/PostMessage成功了),至于那个BCB中嵌套汇编的就先放一放,把这个先给解决了吧...
我不想因为无法模拟键盘鼠标操作而转去其他开发语言啊。。。

[解决办法]
模拟按键可以用keybd_event函数,模拟鼠标可以用mouse_event函数。
另外有个SendInput函数,可以完成上面2种功能。msdn说,在NT系统下,推荐使用这个函数。
[解决办法]
SendMessage() 就可以,不成功多数是HWND不正确

你可以先用`记事本`做测试看看是否可以模拟打字
[解决办法]
unit sndkey32;

interface

Uses SysUtils, Windows, Messages;

Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean;
function AppActivate(WindowName : PChar) : boolean;

{Buffer for working with PChar 's}

const
WorkBufLen = 40;
var
WorkBuf : array[0..WorkBufLen] of Char;

implementation
type
THKeys = array[0..pred(MaxLongInt)] of byte;
var
AllocationSize : integer;

(*
Converts a string of characters and key names to keyboard events and
passes them to Windows.

Example syntax:

SendKeys( 'abc123{left}{left}{left}def{end}456{left 6}ghi{end}789 ', True);

*)

Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean;
type
WBytes = array[0..pred(SizeOf(Word))] of Byte;

TSendKey = record
Name : ShortString;
VKey : Byte;
end;

const
{Array of keys that SendKeys recognizes.

If you add to this list, you must be sure to keep it sorted alphabetically
by Name because a binary search routine is used to scan it.}

MaxSendKeyRecs = 41;
SendKeyRecs : array[1..MaxSendKeyRecs] of TSendKey =
(
(Name: 'BACKSPACE '; VKey:VK_BACK),
(Name: 'BKSP '; VKey:VK_BACK),
(Name: 'BREAK '; VKey:VK_CANCEL),
(Name: 'BS '; VKey:VK_BACK),
(Name: 'CAPSLOCK '; VKey:VK_CAPITAL),
(Name: 'CLEAR '; VKey:VK_CLEAR),
(Name: 'DEL '; VKey:VK_DELETE),
(Name: 'DELETE '; VKey:VK_DELETE),
(Name: 'DOWN '; VKey:VK_DOWN),
(Name: 'END '; VKey:VK_END),
(Name: 'ENTER '; VKey:VK_RETURN),
(Name: 'ESC '; VKey:VK_ESCAPE),
(Name: 'ESCAPE '; VKey:VK_ESCAPE),
(Name: 'F1 '; VKey:VK_F1),
(Name: 'F10 '; VKey:VK_F10),
(Name: 'F11 '; VKey:VK_F11),
(Name: 'F12 '; VKey:VK_F12),
(Name: 'F13 '; VKey:VK_F13),
(Name: 'F14 '; VKey:VK_F14),
(Name: 'F15 '; VKey:VK_F15),
(Name: 'F16 '; VKey:VK_F16),
(Name: 'F2 '; VKey:VK_F2),
(Name: 'F3 '; VKey:VK_F3),
(Name: 'F4 '; VKey:VK_F4),
(Name: 'F5 '; VKey:VK_F5),
(Name: 'F6 '; VKey:VK_F6),
(Name: 'F7 '; VKey:VK_F7),
(Name: 'F8 '; VKey:VK_F8),
(Name: 'F9 '; VKey:VK_F9),
(Name: 'HELP '; VKey:VK_HELP),
(Name: 'HOME '; VKey:VK_HOME),
(Name: 'INS '; VKey:VK_INSERT),
(Name: 'LEFT '; VKey:VK_LEFT),
(Name: 'NUMLOCK '; VKey:VK_NUMLOCK),


(Name: 'PGDN '; VKey:VK_NEXT),
(Name: 'PGUP '; VKey:VK_PRIOR),
(Name: 'PRTSC '; VKey:VK_PRINT),
(Name: 'RIGHT '; VKey:VK_RIGHT),
(Name: 'SCROLLLOCK '; VKey:VK_SCROLL),
(Name: 'TAB '; VKey:VK_TAB),
(Name: 'UP '; VKey:VK_UP)
);

{Extra VK constants missing from Delphi 's Windows API interface}
VK_NULL=0;
VK_SemiColon=186;
VK_Equal=187;
VK_Comma=188;
VK_Minus=189;
VK_Period=190;
VK_Slash=191;
VK_BackQuote=192;
VK_LeftBracket=219;
VK_BackSlash=220;
VK_RightBracket=221;
VK_Quote=222;
VK_Last=VK_Quote;

ExtendedVKeys : set of byte =
[VK_Up,
VK_Down,
VK_Left,
VK_Right,
VK_Home,
VK_End,
VK_Prior, {PgUp}
VK_Next, {PgDn}
VK_Insert,
VK_Delete];

const
INVALIDKEY = $FFFF {Unsigned -1};
VKKEYSCANSHIFTON = $01;
VKKEYSCANCTRLON = $02;
VKKEYSCANALTON = $04;
UNITNAME = 'SendKeys ';
var
UsingParens, ShiftDown, ControlDown, AltDown, FoundClose : Boolean;
PosSpace : Byte;
I, L : Integer;
NumTimes, MKey : Word;
KeyString : String[20];

procedure DisplayMessage(Message : PChar);
begin
MessageBox(0,Message,UNITNAME,0);
end;

function BitSet(BitTable, BitMask : Byte) : Boolean;
begin
Result:=ByteBool(BitTable and BitMask);
end;

procedure SetBit(var BitTable : Byte; BitMask : Byte);
begin
BitTable:=BitTable or Bitmask;
end;

Procedure KeyboardEvent(VKey, ScanCode : Byte; Flags : Longint);
var
KeyboardMsg : TMsg;
begin
keybd_event(VKey, ScanCode, Flags,0);
If (Wait) then While (PeekMessage(KeyboardMsg,0,WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)) do begin
TranslateMessage(KeyboardMsg);
DispatchMessage(KeyboardMsg);
end;
end;

Procedure SendKeyDown(VKey: Byte; NumTimes : Word; GenUpMsg : Boolean);
var
Cnt : Word;
ScanCode : Byte;
NumState : Boolean;
KeyBoardState : TKeyboardState;
begin
If (VKey=VK_NUMLOCK) then begin
NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(KeyBoardState);
If NumState then KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1)
else KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(KeyBoardState);
exit;
end;

ScanCode:=Lo(MapVirtualKey(VKey,0));
For Cnt:=1 to NumTimes do
If (VKey in ExtendedVKeys)then begin
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);
If (GenUpMsg) then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end else begin
KeyboardEvent(VKey, ScanCode, 0);
If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end;
end;

Procedure SendKeyUp(VKey: Byte);
var
ScanCode : Byte;
begin
ScanCode:=Lo(MapVirtualKey(VKey,0));
If (VKey in ExtendedVKeys)then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)
else KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end;

Procedure SendKey(MKey: Word; NumTimes : Word; GenDownMsg : Boolean);
begin
If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyDown(VK_SHIFT,1,False);
If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyDown(VK_CONTROL,1,False);
If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyDown(VK_MENU,1,False);


SendKeyDown(Lo(MKey), NumTimes, GenDownMsg);
If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyUp(VK_SHIFT);
If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyUp(VK_CONTROL);
If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyUp(VK_MENU);
end;

{Implements a simple binary search to locate special key name strings}

Function StringToVKey(KeyString : ShortString) : Word;
var
Found, Collided : Boolean;
Bottom, Top, Middle : Byte;
begin
Result:=INVALIDKEY;
Bottom:=1;
Top:=MaxSendKeyRecs;
Found:=false;
Middle:=(Bottom+Top) div 2;
Repeat
Collided:=((Bottom=Middle) or (Top=Middle));
If (KeyString=SendKeyRecs[Middle].Name) then begin
Found:=True;
Result:=SendKeyRecs[Middle].VKey;
end else begin
If (KeyString> SendKeyRecs[Middle].Name) then Bottom:=Middle
else Top:=Middle;
Middle:=(Succ(Bottom+Top)) div 2;
end;
Until (Found or Collided);
If (Result=INVALIDKEY) then DisplayMessage( 'Invalid Key Name ');
end;
[解决办法]
hwd 是窗口句柄,x,y 是你要点击的窗口坐标(相对的)。
hwd 是屏幕句柄,x,y 就是屏幕坐标(绝对的)。
PostMessage(hwd,WM_LBUTTONDOWN,MK_LBUTTON,MAKELONG(x,y));
Sleep(100); //这里必须要加延时,多少你可以调整。不能太小,也不太多(1秒以内)。
PostMessage(hwd,WM_LBUTTONUP,MK_LBUTTON,MAKELONG(x,y));

热点排行