NumLock・CapsLock・ScrollLockのオン・オフ(T) <TOP>
NumLock・CapsLock・ScrollLockをON/OFFさせます。
GetVersionEx オペレーティングシステムの種類やバージョンに関する情報を取得
keybd_event 特殊キーの状態を設定
GetKeyboardState 仮想キーボードのキーの状態を取得
SetKeyboardState キーボードの仮想キーの状態を設定
Windows9x系とWindowsNT系では異なるためOSバージョンを取得し分けて操作しています。
'================================================================ '= CapsLock・NumLock・ScrollLockのオン・オフ
'= (CapsLock.bas) '================================================================ #include "Windows.bi" Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type ' オペレーティングシステムの種類やバージョンに関する情報を取得 Declare Function Api_GetVersionEx& Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) ' 特殊キーの状態を設定 Declare Sub Api_keybd_event Lib "user32" Alias "keybd_event" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags&, ByVal dwExtraInfo&) ' 仮想キーボードのキーの状態を取得 Declare Function Api_GetKeyboardState& Lib "user32" Alias "GetKeyboardState" (pbKeyState As Byte) ' キーボードの仮想キーの状態を設定 Declare Function Api_SetKeyboardState& Lib "user32" Alias "SetKeyboardState" (lppbKeyState As Byte) #define VK_NUMLOCK &H90 '[NumLock] #define VK_CAPITAL &H14 '[Caps Lock] #define VK_SCROLL &H91 '[Scroll] #define KEYEVENTF_KEYUP &H2 'キーを放す #define KEYEVENTF_EXTENDEDKEY &H1 'スキャンコードにプリフィックスバイト0xE0(224)を付加 #define VER_PLATFORM_WIN32_NT 2 'WINDOWSNT、2000、XP #define VER_PLATFORM_WIN32_WINDOWS 1 'WINDOWS9x Var Shared Text(2) As Object Var Shared Button(2) As Object For i = 0 To 2 Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14 Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i + 1))) : Button(i).SetFontSize 14 Next Var Shared State(2) As Integer Var Shared VK(2) As Long Var Shared NumLockState As Integer Var Shared CapsLockState As Integer Var Shared ScrollLockState As Integer Var Shared Index As Integer '================================================================ '= ScrollLock '================================================================ Declare Sub Lock_Toggle () Sub Lock_Toggle() Var OsVer As OSVERSIONINFO Var keys(255) As Byte Var Ret As Long OsVer.dwOSVersionInfoSize = Len(OsVer) Ret = Api_GetVersionEx(OsVer) Ret = Api_GetKeyboardState(keys(0)) State(Index) = keys(VK(Index)) If State(Index) = 0 Then 'Lock(OFF→ON) If OsVer.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then 'Win9x keys(VK(Index)) = 1 Ret = Api_SetKeyboardState(keys(0)) Else If OsVer.dwPlatformId = VER_PLATFORM_WIN32_NT Then 'WinNT Api_keybd_event VK(Index), &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0 Api_keybd_event VK(Index), &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0 End If Text(Index).SetWindowText "ON" Else 'Lock(ON→OFF) If OsVer.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then 'Win9x keys(VK(Index)) = 0 Ret = Api_SetKeyboardState(keys(0)) Else If OsVer.dwPlatformId = VER_PLATFORM_WIN32_NT Then 'WinNT Api_keybd_event VK(Index), &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0 Api_keybd_event VK(Index), &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0 End If Text(Index).SetWindowText "OFF" End If End Sub '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() VK(0) = VK_NUMLOCK VK(1) = VK_CAPITAL VK(2) = VK_SCROLL State(0) = NumLockState State(1) = CapsLockState State(2) = ScrollLockState End Sub '================================================================ '= NumLock '================================================================ Declare Sub Button1_on edecl () Sub Button1_on() Index = 0 Lock_Toggle End Sub '================================================================ '= CapsLock '================================================================ Declare Sub Button2_on edecl () Sub Button2_on() Index = 1 Lock_Toggle End Sub '================================================================ '= ScrollLock '================================================================ Declare Sub Button3_on edecl () Sub Button3_on() Index = 2 Lock_Toggle End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End