網頁

2013年5月7日 星期二

[雜事]2013-05-07 VB 實現 控制numlock capslock scrolllock 燈號及狀態

找了好久才找到一個 終於可以真正實現的方法

這個功能是要 不但可以控制 numlock capslock scrolllock 的狀態

並且還要能夠 控制 鍵盤燈號

一般VB6 有很多可以直接設置的

SendKeys.Send"{TAB}"

像上面這種就是直接傳值給電腦我按了tab這個動作

但是鍵盤並沒有接收到這樣的指令

因此燈號根本不會變動

後來在網路上找到了這種方法:








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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2

Public Sub SetNumLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyNumlock) And vbShiftMask)
If i <> bLock Then
   ScanCode = MapVirtualKey(vbKeyNumlock, 0)
   Call keybd_event(vbKeyNumlock, ScanCode, 0, 0)
   Call keybd_event(vbKeyNumlock, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub
Public Sub SetScrollLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyScrollLock) And vbShiftMask)
If i <> bLock Then
   ScanCode = MapVirtualKey(vbKeyScrollLock, 0)
   Call keybd_event(vbKeyScrollLock, ScanCode, 0, 0)
   Call keybd_event(vbKeyScrollLock, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub
Public Sub SetCapsLock(ByVal bLock As Boolean)
Dim i As Boolean, ScanCode As Long
i = CBool(GetKeyState(vbKeyCapital) And vbShiftMask)
If i <> bLock Then
   ScanCode = MapVirtualKey(vbKeyCapital, 0)
   Call keybd_event(vbKeyCapital, ScanCode, 0, 0)
   Call keybd_event(vbKeyCapital, ScanCode, KEYEVENTF_KEYUP, 0)
End If
End Sub





最後設置幾個按鈕

按鈕看要怎麼樣切換 就怎麼樣設置

Call SetCapsLock(True)


像這樣就搞定

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。

Site Meter