使用API模擬滑鼠事件

2021-04-25 11:43:28 字數 2095 閱讀 7885

code:

private declare sub mouse_event lib "user32" (byval dwflags as long, _

byval dx as long, _

byval dy as long, _

byval cbuttons as long, _

byval dwextrainfo as long)

const mouseeventf_leftdown as short = &h2s

const mouseeventf_leftup as short = &h4s

const mouseeventf_middledown as short = &h20s

const mouseeventf_middleup as short = &h40s

const mouseeventf_move as short = &h1s

const mouseeventf_absolute as short = &h8000s

const mouseeventf_rightdown as short = &h8s

const mouseeventf_rightup as short = &h10s

先在程式中加入如下的宣告後,就能引用mouse_event函式:

private declare sub mouse_event lib "user32" (byval dwflags as long, byval dx as long, byval dy as long, byval cbuttons as long, byval dwextrainfo as long)

其中各個引數的意義如下表所示: 引數

意義

dwflags

long,下表中標誌之一或它們的組合

dx,dy

long,根據mouseeventf_absolute標誌,指定x,y方向的絕對位置或相對位置

cbuttons

long,沒有使用

dwextrainfo

long,沒有使用

dwflags常數

意義

mouseeventf_move

&h1,移動滑鼠

mouseeventf_leftdown

&h2,模擬滑鼠左鍵按下

mouseeventf_leftup

&h4,模擬滑鼠左鍵抬起

mouseeventf_rightdown

&h8,模擬滑鼠右鍵按下

mouseeventf_rightup

&h10,模擬滑鼠右鍵抬起

mouseeventf_middledown

&h20,模擬滑鼠中鍵按下

mouseeventf_middleup

&h40,模擬滑鼠中鍵抬起

mouseeventf_absolute

&h8000,標示是否採用絕對座標

程式中我們這樣使用mouse_event函式:

1、這裡是滑鼠左鍵按下和鬆開兩個事件的組合即一次單擊:

mouse_event mouseeventf_leftdown or mouseeventf_leftup, 0, 0, 0, 0

2、模擬滑鼠右鍵單擊事件:

mouse_event mouseeventf_rightdown or mouseeventf_rightup, 0, 0, 0, 0

3、兩次連續的滑鼠左鍵單擊事件 構成一次滑鼠雙擊事件:

mouse_event mouseeventf_leftdown or mouseeventf_leftup, 0, 0, 0, 0

mouse_event mouseeventf_leftdown or mouseeventf_leftup, 0, 0, 0, 0

如上面所示如果我們要使用兩個dwflags常數的組合時,可以用or將其連線起來。把dx,dy引數都設為0,是指每次模擬事件的位置是滑鼠的當前位置,dx,dy在沒有使用mouseeventf_absolute標誌時是相對於滑鼠當前位置的座標,如果設定為使用絕對座標,則dx,dy是相對於螢幕的座標。

文章出自:http://www.111ccc.cn/computer/2007/1201/article_452.html

模擬鍵盤事件與滑鼠事件

模擬事件 title head body button id mouseeventtrigger 鍵盤模擬按鈕事件 button button id keyboardeventtrigger 按鈕模擬鍵盤事件 button body script varbtn1 document.getelemen...

C 模擬鍵盤滑鼠事件

1.模擬鍵盤事件 system.windows.forms.sendkeys 以下是 sendkeys 的一些特殊鍵 表。鍵 backspace 或 break caps lock del 或 delete 或 down arrow 下箭頭鍵 end enter 或 esc help home in...

Unity中模擬滑鼠事件

using system public class mousesimulater endregion unity螢幕座標從左下角開始,向右為x軸,向上為y軸 windows螢幕座標從左上角開始,向右為x軸,向下為y軸 移動滑鼠到指定位置 使用unity螢幕座標而不是windows螢幕座標 publi...