|
-
Nov 6th, 2001, 03:56 PM
#1
Thread Starter
Lively Member
keybd_event Function
I have been playing around just to see how things work. I thought I might try to turn on Caps Lock, Scroll Lock, and Num Lock in code... pretty simple stuff really, but I just wanted to try. Anyway, I can't get Num Lock to work. I've done it the same as the others. Anyone know why? I thought that possibly the virtual key constant for VK_NUMLOCK might be wrong... but I don't know. I used http://www.vbapi.com/ref/other/virtualkeycodes.html as my resource.
Here's my code:
Private Declare Sub keybd_event Lib "user32.dll" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const KEYEVENTF_KEYUP = &H2
Const VK_CAPITAL = &H14
Const VK_NUMLOCK = &H90
Const VK_OEM_SCROLL = &H91
'press Caps Lock
Private Sub Command1_Click()
keybd_event VK_CAPITAL, 0, 0, 0
keybd_event VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0
End Sub
'press Num Lock
Private Sub Command2_Click()
keybd_event VK_NUMLOCK, 0, 0, 0
keybd_event VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0
End Sub
'press Scroll Lock
Private Sub Command3_Click()
keybd_event VK_OEM_SCROLL, 0, 0, 0
keybd_event VK_OEM_SCROLL, 0, KEYEVENTF_KEYUP, 0
End Sub
-
Nov 6th, 2001, 04:22 PM
#2
Thread Starter
Lively Member
With further research, I found that keybd_event is obselete in Win98, so I tried the SendInput Function instead. I got the exact same result; Caps & Scroll will work, but Num will not. I did use the same virtual key constant as in the first example...
Const VK_NUMLOCK = &H90
-
Nov 7th, 2001, 03:08 AM
#3
I found the following code here , and it works for numlock as well:
Code:
Option Explicit
Private Type KeyboardByteArray
KeyByte(0 To 255) As Byte
End Type
Private KeyboardArray As KeyboardByteArray
Private Declare Function GetKeyboardState Lib "user32" (KeyboardArray As KeyboardByteArray) As Long
Private Declare Function SetKeyboardState Lib "user32" (KeyboardArray As KeyboardByteArray) As Long
Private Const VK_CAPITAL = &H14
Private Const VK_NUMLOCK = &H90
Private Const VK_SCROLL = &H91
Private Sub ToggleCapsLock()
GetKeyboardState KeyboardArray
KeyboardArray.KeyByte(VK_CAPITAL) = IIf(KeyboardArray.KeyByte(VK_CAPITAL) = 1, 0, 1)
SetKeyboardState KeyboardArray
End Sub
Private Sub CapsLockOn()
GetKeyboardState KeyboardArray
KeyboardArray.KeyByte(VK_CAPITAL) = 1
SetKeyboardState KeyboardArray
End Sub
Private Sub CapsLockOff()
GetKeyboardState KeyboardArray
KeyboardArray.KeyByte(VK_CAPITAL) = 0
SetKeyboardState KeyboardArray
End Sub
-
Nov 7th, 2001, 07:21 AM
#4
Or, try this
VB Code:
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
Private Const VK_CAPITAL = &H14
Private Const VK_NUMLOCK = &H90
Private Const VK_SCROLL = &H91
ReDim KeyboardBuffer(256) As Byte
GetKeyboardState KeyboardBuffer(0)
'This example show you how to press the Caps Lock button. if you want
'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL'
'with 'VK_NUMLOCK' or VK_SCROLL as desired.
If KeyboardBuffer(VK_CAPITAL) And 1 Then
KeyboardBuffer(VK_CAPITAL) = 0
Else
KeyboardBuffer(VK_CAPITAL) = 1
End If
SetKeyboardState KeyboardBuffer(0)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|