vb自动按键

vb自动按键,第1张

添加2个Timer控件,属性默认,Timer1用来设置热键F9和F10,Timer2用来设置连续按键之间的间隔时间,程序后台有效,但是对于游戏不敢保证,你自己测试,

Option Explicit

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Const KEYEVENTF_KEYUP = &H2 '释放按键常数

Function MyKey(vKeyCode) As Boolean

MyKey = GetAsyncKeyState(vKeyCode) < 0

End Function

Private Sub Form_Load()

Timer1Enabled = True

Timer1Interval = 50

Timer2Enabled = True

Timer2Interval = 1000

End Sub

Private Sub Timer1_Timer()

If MyKey(vbKeyF9) Then Timer2 = True '开始连续按"键"

If MyKey(vbKeyF10) Then Timer2 = False '停止

End Sub

Private Sub Timer2_Timer()

Call keybd_event(88, 0, 0, 0) '按下

Call keybd_event(88, 0, KEYEVENTF_KEYUP, 0) '释放

End Sub

Shell () 函数额可以调用第三方程序。 shutdown是windows管理关机的程序,直接调用

Shell "cmdexe /c shutdown -s -t 10 -c"

另外,你说要别的办法,用windows API: ExitWindowsEx

ExitWindowsEx需要获取权限

主要代码:

Private Sub Form_Load()

Dim hProcessHandle As Long

Dim hTokenHandle As Long

Dim tmpLuid As LUID

Dim tkpNew As TOKEN_PRIVILEGES

Dim tkpPrevious As TOKEN_PRIVILEGES

Dim lBufferNeeded As Long

'获取当前进程的一个伪句柄

hProcessHandle = GetCurrentProcess()

OpenProcessToken hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle

LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

tkpNewPrivilegeCount = 1

tkpNewTheLuid = tmpLuid

tkpNewAttributes = SE_PRIVILEGE_ENABLED

lBufferNeeded = 0

'允许当前应用程序有关闭 *** 作系统的权限

AdjustTokenPrivileges hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded

End Sub

Private Sub Command1_Click()

'重新启动计算机

ExitWindowsEx EWX_REBOOT, 0

End Sub

Private Sub Command2_Click()

'关闭计算机

ExitWindowsEx EWX_FORCE Or EWX_SHUTDOWN, 0

End Sub

Private Sub Command4_Click()

ExitWindowsEx EWX_LOGOFF, 0

End Sub

需要填一个label控件

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyUp Then

Label1Caption = "UP"

ElseIf KeyCode = vbKeyDown Then

Label1Caption = "DOWN"

ElseIf KeyCode = vbKeyLeft Then

Label1Caption = "LEFT"

ElseIf KeyCode = vbKeyRight Then

Label1Caption = "RIGHT"

End If

End Sub

Private Sub Command1_Click()   '执行按键

Timer1Interval = 5000

Timer1Enabled = True

Timer1_Timer

End Sub

Private Sub Command2_Click()   '结束按键

Timer1Enabled = False

MeCls

Print "程序运行结束"

End Sub

Private Sub Timer1_Timer()

Static x As Long

x = x + 1

MeCls

Print "程序正在运行……" & x

'要执行的程序放在这里

End Sub

上述代码涉及三个控件:Command1、Command2、Timer1

以上就是关于vb自动按键全部的内容,包括:vb自动按键、VB里面单击按钮运行程序、vb 程序检测按键等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9658014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存