在游戏里不能用mouse_event来模拟鼠标。。。请教`

a231dcf3s25f43 2005-04-06 06:39:41
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,0,0,0,0);
这是一个DX游戏``我想在当前位置里单击``可是不行啊```晕``请教还有没有别的办法啊??
...全文
1992 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gohappy_1999 2005-07-29
  • 打赏
  • 举报
回复
帮你顶
Mr-Chen 2005-07-29
  • 打赏
  • 举报
回复
用应用程序模拟键盘和鼠标按键

作者:Owen.Guo VC知识库



 在Windows大行其道的今天,windows界面程序受到广大用户的欢迎。对这些程序的操作不外乎两种,键盘输入控制和鼠标输入控制。有时,对于繁杂的,或重复性的输入操作,我们能否通过编制程序来代替手工输入,而用程序来模拟键盘及鼠标的输入呢?答案是肯定的。这里主要是通过两个Windows API函数来实现的。下面以 VC++为例来介绍一下如何实现这两个功能。


下载本文示例代码


模拟键盘我们用Keybd_event这个api函数,模拟鼠标按键用mouse_event函数。在VC里调用api函数是既简单又方便不过的事了。

  首先介绍一下Keybd_event函数。Keybd_event能触发一个按键事件,也就是说回产生一个WM_KEYDOWN或WM_KEYUP消息。当然也可以用产生这两个消息来模拟按键,但是没有直接用这个函数方便。Keybd_event共有四个参数,第一个为按键的虚拟键值,如回车键为 vk_return, tab键为vk_tab。第二个参数为扫描码,一般不用设置,用0代替就行第三个参数为选项标志,如果为keydown则置0即可,如果为keyup则设成“KEYEVENTF_KEYUP”,第四个参数一般也是置0即可。用如下代码即可实现模拟按下键,其中的XX表示XX键的虚拟键值,在这里也就是各键对应的键码,如’A’=65

keybd_event(65,0,0,0);

keybd_event(65,0,KEYEVENTF_KEYUP,0); ...

  mouse_event最好配合SetCursorPos(x,y)函数一起使用,与Keybd_event类似,mouse_event有五个参数,第一个为选项标志,为MOUSEEVENTF_LEFTDOWN时表示左键按下为MOUSEEVENTF_LEFTUP表示左键松开,向系统发送相应消息。第二三个参数分别表示x,y

相对位置,一般可设为0,0,第四五个参数并不重要,一般也可设为0,0。若要得到Keybd_event和mouse_event函数的更详细的用法,可以查阅msdn或delphi帮助。


下面是关于mouse_event的示例代码:


POINT lpPoint;

GetCursorPos(&lpPoint);

SetCursorPos(lpPoint.x, lpPoint.y);

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);

mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);


  上面的代码表示鼠标的双击,若要表示单击,用两个mouse_event即可(一次放下,一次松开)。  

  注意,不管是模拟键盘还是鼠标事件,都要注意还原,即按完键要松开,一个keydown对应一个keyup;鼠标单击完也要松开, 不然可能影响程序的功能。
temp0001 2005-07-29
  • 打赏
  • 举报
回复
德国货币
EagleTwenty 2005-07-25
  • 打赏
  • 举报
回复
大家说的都是些老套套,我也试过楼主的事,在连连看游戏里真的只能点一下,第二下就是点不下去,不知道为什么。
Anikan 2005-07-25
  • 打赏
  • 举报
回复
是可以的,我在DX的游戏中使用过。你的函数调用错了
应该是:
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
//稍微加一点延时间。因为真的鼠标大约8ms采样一次。有的游戏在计算单击的时候,可能需要时间 戳
Sleep(8);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
日总是我哥 2005-07-25
  • 打赏
  • 举报
回复
mark
AntonlioX 2005-07-25
  • 打赏
  • 举报
回复
mark

帮你up
goodheartppl 2005-04-07
  • 打赏
  • 举报
回复
MOUSEMOVEPOINT Structure

--------------------------------------------------------------------------------

The MOUSEMOVEPOINT structure contains information about the mouse's location in screen coordinates.

Syntax

typedef struct tagMOUSEMOVEPOINT {
int x;
int y;
DWORD time;
ULONG_PTR dwExtraInfo;
} MOUSEMOVEPOINT, *PMOUSEMOVEPOINT;
Members

x
Specifies the x-coordinate of the mouse.
y
Specifies the y-coordinate of the mouse.
time
Specifies the time stamp of the mouse coordinate.
dwExtraInfo
Specifies extra information associated with this coordinate.
Structure Information

Header Declared in Winuser.h, include Windows.h
Minimum operating systems Millennium, Windows 2000
goodheartppl 2005-04-07
  • 打赏
  • 举报
回复
GetCursorPos Function

--------------------------------------------------------------------------------

The GetCursorPos function retrieves the cursor's position, in screen coordinates.

Syntax

BOOL GetCursorPos( LPPOINT lpPoint
);
Parameters

lpPoint
[out] Pointer to a POINT structure that receives the screen coordinates of the cursor.
Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.




Remarks

The cursor position is always specified in screen coordinates and is not affected by the mapping mode of the window that contains the cursor.

The calling process must have WINSTA_READATTRIBUTES access to the window station.

Example

For an example, see Using the Keyboard to Move the Cursor.

Function Information

Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
CrazyAzreal 2005-04-06
  • 打赏
  • 举报
回复
那些什么MOUSEEVENTF_LEFTDOWN的标记``所有的组合我都试过了```楼上说的都是一些基本知识``我都知道、用过``。。
oyljerry 2005-04-06
  • 打赏
  • 举报
回复
1、首先我们要知道现在鼠标的位置(为了好还原现在鼠标的位置)所以我们就要用到API函数GetCursorPos,它的使用方法如下:
BOOL GetCursorPos(

LPPOINT lpPoint // address of structure for cursor position
);
2、我们把鼠标的位置移到要到人物走到的地方,我们就要用到SetCursorPos函数来移动鼠标位置,它的使用方法如下:
BOOL SetCursorPos(

int X, // horizontal position
int Y // vertical position
);
3、模拟鼠标发出按下和放开的动作,我们要用到mouse_event函数来实现,具休使用方法用下:
VOID mouse_event(

DWORD dwFlags, // flags specifying various motion/click variants
DWORD dx, // horizontal mouse position or position change
DWORD dy, // vertical mouse position or position change
DWORD dwData, // amount of wheel movement
DWORD dwExtraInfo // 32 bits of application-defined information
);
在它的dwFlags处,可用的事件很多如移动MOUSEEVENTF_MOVE,左键按下MOUSEEVENTF_LEFTDOWN,左键放开MOUSEEVENTF_LEFTUP,具体的东东还是查一下MSDN吧~~~~~
好了,有了以前的知识,我们就可以来看看人物移走是怎么实现的了:

getcursorpos(point);
setcursorpos(ranpoint(80,windowX),ranpoint(80,windowY));//ranpoint是个自制的随机坐标函数
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
setcursorpos(point.x,point.y);

看了以上的代码,是不是觉得人物的游走很简单啦~~,举一仿三,还有好多好东东可以用这个技巧实现(我早就说过,TMD,这是垃圾外挂的做法,相信了吧~~~),接下来,再看看游戏里面自动攻击的做法吧(必需游戏中攻击支持快捷键的),道理还是一样的,只是用的API不同罢了~~~,这回我们要用到的是keybd_event函数,其用法如下:
VOID keybd_event(

BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
我们还要知道扫描码不可以直接使用,要用函数MapVirtualKey把键值转成扫描码,MapVirtualKey的具体使用方法如下:
UINT MapVirtualKey(

UINT uCode, // virtual-key code or scan code
UINT uMapType // translation to perform
);
好了,比说此快接键是CTRL+A,接下来让我们看看实际代码是怎么写的:

keybd_event(VK_CONTROL,mapvirtualkey(VK_CONTROL,0),0,0);
keybd_event(65,mapvirtualkey(65,0),0,0);
keybd_event(65,mapvirtualkey(65,0),keyeventf_keyup,0);
keybd_event(VK_CONTROL,mapvirtualkey(VK_CONTROL,0),keyeventf_keyup,0);

首先模拟按下了CTRL键,再模拟按下A键,再模拟放开A键,最后放开CTRL键,这就是一个模拟按快捷键的周期。
BOYGUARD110 2005-04-06
  • 打赏
  • 举报
回复
//
// 在鼠标当前位置 鼠标左键单击
//
::mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);// 鼠标左键按下
::mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); // 鼠标左键抬起


你的参数不对吧, MOUSEEVENTF_ABSOLUTE 使用来和 位置 配合使用的
bloodstone 2005-04-06
  • 打赏
  • 举报
回复
你想模拟鼠标、键盘等的事件吗?可以试试SendInput:
The SendInput function synthesizes keystrokes, mouse motions, and button clicks.

UINT SendInput(
UINT nInputs, // count of input events
LPINPUT pInputs, // array of input events
int cbSize // size of structure
);

例如:
(此例表示将鼠标移动到pos,然后右键单击)

int count=0;
INPUT input[3];

input[count].type=INPUT_MOUSE;
input[count].mi.dx=pos.x;
input[count].mi.dy=pos.y;
input[count].mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
input[count].mi.dwExtraInfo=NULL;
input[count].mi.mouseData=NULL;
input[count].mi.time=NULL;
count++;

input[count].type=INPUT_MOUSE;
input[count].mi.dx=pos.x;
input[count].mi.dy=pos.y;
input[count].mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN;
input[count].mi.dwExtraInfo=NULL;
input[count].mi.mouseData=NULL;
input[count].mi.time=NULL;
count++;

input[count].type=INPUT_MOUSE;
input[count].mi.dx=pos.x;
input[count].mi.dy=pos.y;
input[count].mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP;
input[count].mi.dwExtraInfo=NULL;
input[count].mi.mouseData=NULL;
input[count].mi.time=NULL;
count++;

int ret=SendInput(count,input,sizeof(INPUT));
fanqing 2005-04-06
  • 打赏
  • 举报
回复
gz

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧