在宏运行结束时打开NUMLOCK [英] Turning NUMLOCK on at the end of a macro run

查看:163
本文介绍了在宏运行结束时打开NUMLOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码做什么::我有一个代码可以在屏幕上移动鼠标,获取打印屏幕并将其粘贴到excel.

What code does: I have a code that moves the mouse around the screen, takes printscreens and pastes it to excel.

问题:由于某种原因,我的代码总是(绝对没有例外)在每次运行后都会关闭NUMLOCK键.

Problem: For some reason, my code always (with absolutely no exceptions) turns the NUMLOCK key off after every run.

到目前为止我尝试过的操作:我四处搜索并找到了SendKeys(NUMLOCK),它在理论上是可行的(尽管对于用户来说这似乎很成问题).

What I tried so far: I searched around and found the SendKeys (NUMLOCK), which in theory works (although it seems to be very problematic for users).

我要做什么:我想在每次运行宏后打开NUMLOCK,

What I want to do: I want to turn the NUMLOCK on after each macro run,

Obs1::我不知道是什么原因导致宏首先将其关闭.修复导致此问题的方法将是理想的选择,但是由于我不知道问题出在哪里,因此我首先要使代码正常运行.我将尽快找到打开NUMLOCK键的方法.

Obs1: I have no idea what is causing the macro to turn it off in the first place. Fixing whatever is causing this would be ideal, but since I have no idea what the problem is, I first want to get my code functional. I am going to work on that as soon as find a way to turn the NUMLOCK key on.

问题:我可以使用SendKeys这样做吗?我使用是否正确?有更好的方法吗?

Question: Can I do this using the SendKeys? Am I using it properly? Is there a better way?

Obs2:因为它是一个更大的代码,所以一旦解决,我将用整个代码发布另一个问题,然后继续研究导致问题的原因.

Obs2: Since it is a much bigger code, as soon as this is solved, I am going to post another question with the entire code, and go over on what is causing the problem.

代码我正试图提起数字锁定:

Code I am trying to sue to turn numlock on:

Application.Sendkeys (NUMLOCK)

也尝试过:

Application.Sendkeys ("NUMLOCK")

Application.Sendkeys {NUMLOCK}

推荐答案

您可以通过几个Windows API调用直接设置键状态.从 MSDN页面移植为keybd_event函数:

You can set the keystate directly with a couple of Windows API calls. Ported from the MSDN page for keybd_event function:

#If VBA7 Then
    Private Declare PtrSafe Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, _
                                                              ByVal dwFlags As LongPtr, ByVal dwExtraInfo As LongPtr)
    Private Declare PtrSafe Function GetKeyboardState Lib "user32.dll" (ByVal lpKeyState As LongPtr) As Boolean
#Else
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, _
                                                      ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Function GetKeyboardState Lib "user32.dll" (ByVal lpKeyState As Long) As Boolean
#End If  

Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
Private Const KEYEVENTF_KEYUP As Long = &H2
Private Const VK_NUMLOCK As Byte = &H90
Private Const NumLockScanCode As Byte = &H45

Private Sub ToggleNumlock(enabled As Boolean)
    Dim keystate(255) As Byte
    'Test current keyboard state.
    GetKeyboardState (VarPtr(keystate(0)))
    If (Not keystate(VK_NUMLOCK) And enabled) Or (keystate(VK_NUMLOCK) And Not enabled) Then
        'Send a keydown
        keybd_event VK_NUMLOCK, NumLockScanCode, KEYEVENTF_EXTENDEDKEY, 0&
        'Send a keyup
        keybd_event VK_NUMLOCK, NumLockScanCode, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0&
    End If
End Sub

这样称呼:

Sub Example()
    'Turn Numlock off.
    ToggleNumlock False
    'Turn Numlock on.
    ToggleNumlock True
End Sub

这篇关于在宏运行结束时打开NUMLOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆