Results 1 to 4 of 4

Thread: keybd_event Function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Posts
    108

    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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Posts
    108
    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

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Or, try this
    VB Code:
    1. Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    2. Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
    3.  
    4. Private Const VK_CAPITAL = &H14
    5. Private Const VK_NUMLOCK = &H90
    6. Private Const VK_SCROLL = &H91
    7.  
    8.  
    9. ReDim KeyboardBuffer(256) As Byte
    10. GetKeyboardState KeyboardBuffer(0)
    11. 'This example show you how to press the Caps Lock button. if you want
    12. 'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL'
    13. 'with 'VK_NUMLOCK' or VK_SCROLL as desired.
    14. If KeyboardBuffer(VK_CAPITAL) And 1 Then
    15. KeyboardBuffer(VK_CAPITAL) = 0
    16. Else
    17. KeyboardBuffer(VK_CAPITAL) = 1
    18. End If
    19. 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
  •  



Click Here to Expand Forum to Full Width