NumLock・CapsLock・ScrollLockのオン・オフ(U)          <TOP>


NumLock・CapsLock・ScrollLockをON/OFFさせます。WindowsNT系以降

MapVirtualKey 仮想キーコード・ASCII値・スキャンコード間でコードを変換

keybd_event 特殊キーの状態を設定

GetKeyboardState 仮想キーボードのキーの状態を取得

 

 

'================================================================
'= NumLock・CapsLock・ScrollLockのオン・オフ(U)
'=     (MapVirtualKey.bas)WindowsNT以降
'================================================================
#include "Windows.bi"

' 仮想キーコード・ASCII値・スキャンコード間でコードを変換
Declare Function Api_MapVirtualKey& Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode&, ByVal wMapType&)

' 特殊キーの状態を設定
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)

#define KEYEVENTF_EXTENDEDKEY &H1       'スキャンコードにプリフィックスバイト0xE0(224)を付加
#define KEYEVENTF_KEYUP &H2             'キーを放す
#define VK_SCROLL &H91                  '[Scroll]
#define VK_NUMLOCK &H90                 '[NumLock]
#define VK_CAPITAL &H14                 '[Caps Lock]

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

'================================================================
'= NumLock
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var state(255) As Byte
    Var Ret As Long

    Api_keybd_event VK_NUMLOCK, Api_MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0
    Api_keybd_event VK_NUMLOCK, Api_MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0

    Ret = Api_GetKeyboardState(state(0))
    If state(VK_NUMLOCK) And &H1 Then
        Text(0).SetWindowText "OFF"
    Else
        Text(0).SetWindowText "ON"
    End If
End Sub

'================================================================
'= CapsLock
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Var state(255) As Byte
    Var Ret As Long

    Api_keybd_event VK_CAPITAL, Api_MapVirtualKey(VK_CAPITAL, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0
    Api_keybd_event VK_CAPITAL, Api_MapVirtualKey(VK_CAPITAL, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0

    Ret = Api_GetKeyboardState(state(0))
    If state(VK_CAPITAL) And &H1 Then
        Text(1).SetWindowText "OFF"
    Else
        Text(1).SetWindowText "ON"
    End If
End Sub

'================================================================
'= ScrollLock
'================================================================
Declare Sub Button3_on edecl ()
Sub Button3_on()
    Var state(255) As Byte
    Var Ret As Long

    Api_keybd_event VK_SCROLL, Api_MapVirtualKey(VK_SCROLL, 0), KEYEVENTF_EXTENDEDKEY Or 0, 0
    Api_keybd_event VK_SCROLL, Api_MapVirtualKey(VK_SCROLL, 0), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0

    Ret = Api_GetKeyboardState(state(0))
    If state(VK_SCROLL) And &H1 Then
        Text(2).SetWindowText "OFF"
    Else
        Text(2).SetWindowText "ON"
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End