ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
Python自动化办公应用大全 Excel 2021函数公式学习大典 Kutools for Office 套件发布 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 7271|回复: 9

[求助]在VBA如何获知当前键盘“NumLock”“ CapsLock”“ ScrollLock” 按键的的状

[复制链接]

TA的精华主题

TA的得分主题

发表于 2004-5-11 07:40 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
在VBA如何获知当前键盘“NumLock”“ CapsLock”“ ScrollLock” 按键的的状态,怎样 利用程序编程改变它状态?
如果不能再VBA中直接实现,那如果调用API函数的话,该调用哪些函数?
-- 我是新生,还要好好学习,才能天天向上

TA的精华主题

TA的得分主题

发表于 2004-5-11 08:19 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

Private Type KeyboardBytes kbByte(0 To 255) As Byte End Type

Declare Function GetKeyboardState Lib "user32" (pbKeyState As KeyboardBytes) As Long

Sub test() Dim kbarray As KeyboardBytes GetKeyboardState kbarray MsgBox "caps lock=" & kbarray.kbByte(20) MsgBox "num lock=" & kbarray.kbByte(144) MsgBox "scroll lock=" & kbarray.kbByte(145) End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-5-12 07:43 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

感谢2楼的答复,那再请教,怎样改变他们的状态?

我在VBA中试着用Sendkeys "{NUMLOCK}",模拟键盘输入,但是没有用,这三盏灯不会变。

我本来想根据原先这三个键的当前状态,然后用程序改变它。

现在可以获知键的状态,但怎样用程序改变它?我对API不熟,手头也没有这方面的资料,希望知道的能告知一下,非常感谢。

TA的精华主题

TA的得分主题

发表于 2004-5-12 09:08 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
以下是引用ningbo在2004-5-12 7:43:00的发言:

感谢2楼的答复,那再请教,怎样改变他们的状态?

我在VBA中试着用Sendkeys "{NUMLOCK}",模拟键盘输入,但是没有用,这三盏灯不会变。

我本来想根据原先这三个键的当前状态,然后用程序改变它。

现在可以获知键的状态,但怎样用程序改变它?我对API不熟,手头也没有这方面的资料,希望知道的能告知一下,非常感谢。

用application.sendkeys即可. 問題是僅在runtime有效. 試試....

Sub test() Dim kbarray As KeyboardBytes Application.SendKeys "{numlock}" '使numlock燈亮.顯示狀態卻為0. GetKeyboardState kbarray MsgBox "caps lock=" & kbarray.kbByte(20) MsgBox "num lock=" & kbarray.kbByte(144) MsgBox "scroll lock=" & kbarray.kbByte(145) End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-5-17 09:33 | 显示全部楼层

问题已解决,解决方案详见微软的HOWT Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys http://support.microsoft.com/?id=177674

虽然是英文,但是很简单,代码一看就懂

-- 我是新生,还要好好学习,才能天天向上

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-5-17 19:19 | 显示全部楼层

HOWT Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

适用于
This article was previously published under Q177674

SUMMARY

This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL LOCK keys under Windows 95, Windows 98, Windows Me, Windows NT, or Windows 2000.

MORE INFORMATION

To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the following logic:
  1. Use the GetKeyboardState function to determine the state of the key.
  2. Determine which operating system is being used with the GetVersionEx API. (Windows 95/98/Me and Windows NT/2000 require different methods for toggling these keys.)
  3. Under Windows 95, Windows 98, or Windows Me, use the SetKeyboardState API function to set the state of the key. Under Windows NT or Windows 2000, use the keybd_event function to simulate a key press.
This example shows how to toggle these three keys to "on" if they are "off." This sample may be easily modified to toggle them off or just to check their state.

Sample Project

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a CommandButton to Form1.
  3. Add the following code to the General Declarations section of Form1:
          ' Declare Type for API call:      Private Type OSVERSIONINFO        dwOSVersionInfoSize As Long        dwMajorVersion As Long        dwMinorVersion As Long        dwBuildNumber As Long        dwPlatformId As Long        szCSDVersion As String * 128   '  Maintenance string for PSS usage      End Type      ' API declarations:      Private Declare Function GetVersionEx Lib "kernel32" _         Alias "GetVersionExA" _         (lpVersionInformation As OSVERSIONINFO) As Long      Private Declare Sub keybd_event Lib "user32" _         (ByVal bVk As Byte, _          ByVal bScan As Byte, _          ByVal dwFlags As Long, ByVal dwExtraInfo As Long)      Private Declare Function GetKeyboardState Lib "user32" _         (pbKeyState As Byte) As Long      Private Declare Function SetKeyboardState Lib "user32" _         (lppbKeyState As Byte) As Long      ' Constant declarations:      Const VK_NUMLOCK = &H90      Const VK_SCROLL = &H91      Const VK_CAPITAL = &H14      Const KEYEVENTF_EXTENDEDKEY = &H1      Const KEYEVENTF_KEYUP = &H2      Const VER_PLATFORM_WIN32_NT = 2      Const VER_PLATFORM_WIN32_WINDOWS = 1
  4. Add the following code to the Click event of the CommandButton:
        Private Sub Command1_Click()      Dim o As OSVERSIONINFO      Dim NumLockState As Boolean      Dim ScrollLockState As Boolean      Dim CapsLockState As Boolean      o.dwOSVersionInfoSize = Len(o)      GetVersionEx o      Dim keys(0 To 255) As Byte      GetKeyboardState keys(0)      ' NumLock handling:      NumLockState = keys(VK_NUMLOCK)      If NumLockState <> True Then    'Turn numlock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_NUMLOCK) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _             Or KEYEVENTF_KEYUP, 0        End If      End If      ' CapsLock handling:      CapsLockState = keys(VK_CAPITAL)      If CapsLockState <> True Then    'Turn capslock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_CAPITAL) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _             Or KEYEVENTF_KEYUP, 0        End If      End If      ' ScrollLock handling:      ScrollLockState = keys(VK_SCROLL)      If ScrollLockState <> True Then    'Turn Scroll lock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_SCROLL) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _            Or KEYEVENTF_KEYUP, 0        End If      End If    End Sub
  5. Press the F5 key to run the program. Click the CommandButton. The state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be "on."

REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

127190 : HOWT Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

The information in this article applies t

  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
Last Reviewed:5/13/2003 (2.0)
Keywords:kbhowto KB177674

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-5-17 19:21 | 显示全部楼层

HOWT Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

适用于
This article was previously published under Q177674

SUMMARY

This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL LOCK keys under Windows 95, Windows 98, Windows Me, Windows NT, or Windows 2000.

MORE INFORMATION

To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the following logic:
  1. Use the GetKeyboardState function to determine the state of the key.
  2. Determine which operating system is being used with the GetVersionEx API. (Windows 95/98/Me and Windows NT/2000 require different methods for toggling these keys.)
  3. Under Windows 95, Windows 98, or Windows Me, use the SetKeyboardState API function to set the state of the key. Under Windows NT or Windows 2000, use the keybd_event function to simulate a key press.
This example shows how to toggle these three keys to "on" if they are "off." This sample may be easily modified to toggle them off or just to check their state.

Sample Project

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Add a CommandButton to Form1.
  3. Add the following code to the General Declarations section of Form1:
          ' Declare Type for API call:      Private Type OSVERSIONINFO        dwOSVersionInfoSize As Long        dwMajorVersion As Long        dwMinorVersion As Long        dwBuildNumber As Long        dwPlatformId As Long        szCSDVersion As String * 128   '  Maintenance string for PSS usage      End Type      ' API declarations:      Private Declare Function GetVersionEx Lib "kernel32" _         Alias "GetVersionExA" _         (lpVersionInformation As OSVERSIONINFO) As Long      Private Declare Sub keybd_event Lib "user32" _         (ByVal bVk As Byte, _          ByVal bScan As Byte, _          ByVal dwFlags As Long, ByVal dwExtraInfo As Long)      Private Declare Function GetKeyboardState Lib "user32" _         (pbKeyState As Byte) As Long      Private Declare Function SetKeyboardState Lib "user32" _         (lppbKeyState As Byte) As Long      ' Constant declarations:      Const VK_NUMLOCK = &H90      Const VK_SCROLL = &H91      Const VK_CAPITAL = &H14      Const KEYEVENTF_EXTENDEDKEY = &H1      Const KEYEVENTF_KEYUP = &H2      Const VER_PLATFORM_WIN32_NT = 2      Const VER_PLATFORM_WIN32_WINDOWS = 1
  4. Add the following code to the Click event of the CommandButton:
        Private Sub Command1_Click()      Dim o As OSVERSIONINFO      Dim NumLockState As Boolean      Dim ScrollLockState As Boolean      Dim CapsLockState As Boolean      o.dwOSVersionInfoSize = Len(o)      GetVersionEx o      Dim keys(0 To 255) As Byte      GetKeyboardState keys(0)      ' NumLock handling:      NumLockState = keys(VK_NUMLOCK)      If NumLockState <> True Then    'Turn numlock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_NUMLOCK) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _             Or KEYEVENTF_KEYUP, 0        End If      End If      ' CapsLock handling:      CapsLockState = keys(VK_CAPITAL)      If CapsLockState <> True Then    'Turn capslock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_CAPITAL) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _             Or KEYEVENTF_KEYUP, 0        End If      End If      ' ScrollLock handling:      ScrollLockState = keys(VK_SCROLL)      If ScrollLockState <> True Then    'Turn Scroll lock on        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98          keys(VK_SCROLL) = 1          SetKeyboardState keys(0)        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT        'Simulate Key Press          keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0        'Simulate Key Release          keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _            Or KEYEVENTF_KEYUP, 0        End If      End If    End Sub
  5. Press the F5 key to run the program. Click the CommandButton. The state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be "on."

REFERENCES

For additional information, please see the following articles in the Microsoft Knowledge Base:

127190 : HOWT Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

The information in this article applies t

  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
Last Reviewed:5/13/2003 (2.0)
Keywords:kbhowto KB177674

TA的精华主题

TA的得分主题

发表于 2005-9-8 22:03 | 显示全部楼层
以下是引用菱角在2004-5-11 8:19:00的发言:

Private Type KeyboardBytes kbByte(0 To 255) As Byte End Type

Declare Function GetKeyboardState Lib "user32" (pbKeyState As KeyboardBytes) As Long

Sub test() Dim kbarray As KeyboardBytes GetKeyboardState kbarray MsgBox "caps lock=" & kbarray.kbByte(20) MsgBox "num lock=" & kbarray.kbByte(144) MsgBox "scroll lock=" & kbarray.kbByte(145) End Sub

TA的精华主题

TA的得分主题

发表于 2005-9-8 23:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

很好,以学了一招

TA的精华主题

TA的得分主题

发表于 2018-11-15 13:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
可能是版本不对的原因?其实参考4楼的答案直接用sendkeys(要连续发送两条)就可以实现6楼的效果
同时效果也是相同,只能开闭numlock灯的亮灭,不能控制numlock的实际作用
需要找寻解决方案。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-12-21 00:44 , Processed in 0.024939 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表