Results 1 to 9 of 9

Thread: how can I find out if the control key is down?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I find out if the control key is down?

    I wanna know if the user is holding the control key or not...

    there was a class for all this, what happened to it? where did it go? hehe
    btw wasnt there a way to get information about hardware, such as monitory, keyboard, etc using the .NET classes?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    In the KeyDown Event do this :
    VB Code:
    1. If e.Control Then
    2.             'DO whatever
    3. End If

    And for the other question , check the static class . It have 70 different methods about all sys info .
    VB Code:
    1. SystemInformation.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Pirate
    In the KeyDown Event do this :
    VB Code:
    1. If e.Control Then
    2.             'DO whatever
    3. End If

    And for the other question , check the static class . It have 70 different methods about all sys info .
    VB Code:
    1. SystemInformation.
    hmm thanks for the SystemInformation thingie

    umm but is there a way to figure that out any time, without being in the keydown event?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    umm but is there a way to figure that out any time, without being in the keydown event?
    The only way I can think of is : hotkey ! ! How ? can't think now , I need to sleep !

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    umm no screw the hot keys. there is the GetKeyboardState API, but I just dont know how to use it exactly
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Example for GetKeyboardState API :

    VB Code:
    1. Const VK_CAPITAL = &H14
    2. Const VK_NUMLOCK = &H90
    3. Const VK_SCROLL = &H91
    4. Const VK_USED = VK_SCROLL
    5. Private Type KeyboardBytes
    6.      kbByte(0 To 255) As Byte
    7. End Type
    8. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
    9. Private Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
    10. Private Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
    11. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    12. Dim kbArray As KeyboardBytes, CapsLock As Boolean, kbOld As KeyboardBytes
    13. Private Sub Form_Load()
    14.     'KPD-Team 1999
    15.     'URL: [url]http://www.allapi.net/[/url]
    16.     'E-Mail: [email][email protected][/email]
    17.     'Get the current keyboardstate
    18.     GetKeyboardState kbOld
    19.     'Hide the form
    20.     Me.Hide
    21.     MsgBox "Keep your eyes on the little num-, shift- and scrolllock lights on the keyboard."
    22.     TurnOff VK_CAPITAL
    23.     TurnOff VK_NUMLOCK
    24.     TurnOff VK_SCROLL
    25.     Sleep 1000
    26.     TurnOn VK_NUMLOCK
    27.     Sleep 100
    28.     TurnOn VK_CAPITAL
    29.     Sleep 100
    30.     TurnOn VK_SCROLL
    31.     Sleep 300
    32.     TurnOff VK_NUMLOCK
    33.     Sleep 100
    34.     TurnOff VK_CAPITAL
    35.     Sleep 100
    36.     TurnOff VK_SCROLL
    37.     Sleep 500
    38.     TurnOn VK_NUMLOCK
    39.     TurnOn VK_SCROLL
    40.     Sleep 200
    41.     TurnOff VK_NUMLOCK
    42.     TurnOff VK_SCROLL
    43.     Sleep 200
    44.     TurnOn VK_NUMLOCK
    45.     TurnOn VK_SCROLL
    46.     Sleep 200
    47.     TurnOff VK_NUMLOCK
    48.     TurnOff VK_SCROLL
    49.     Sleep 200
    50.     TurnOn VK_CAPITAL
    51.     Sleep 200
    52.     TurnOff VK_CAPITAL
    53.     Sleep 200
    54.     TurnOn VK_CAPITAL
    55.     Sleep 200
    56.     TurnOff VK_CAPITAL
    57.     Sleep 200
    58.     TurnOn VK_NUMLOCK
    59.     TurnOn VK_SCROLL
    60.     Sleep 200
    61.     TurnOff VK_NUMLOCK
    62.     TurnOff VK_SCROLL
    63.     Sleep 200
    64.     TurnOn VK_NUMLOCK
    65.     TurnOn VK_SCROLL
    66.     Sleep 200
    67.     TurnOff VK_NUMLOCK
    68.     TurnOff VK_SCROLL
    69.     Sleep 200
    70.     TurnOn VK_CAPITAL
    71.     Sleep 400
    72.     TurnOff VK_CAPITAL
    73.     Sleep 200
    74.     TurnOn VK_NUMLOCK
    75.     Sleep 100
    76.     TurnOn VK_CAPITAL
    77.     Sleep 100
    78.     TurnOn VK_SCROLL
    79.     Sleep 300
    80.     TurnOff VK_SCROLL
    81.     Sleep 100
    82.     TurnOff VK_CAPITAL
    83.     Sleep 100
    84.     TurnOff VK_NUMLOCK
    85.     Sleep 1000
    86.     Unload Me
    87. End Sub
    88. Private Sub TurnOn(vkKey As Long)
    89.     'Get the keyboard state
    90.     GetKeyboardState kbArray
    91.     'Change a key
    92.     kbArray.kbByte(vkKey) = 1
    93.     'Set the keyboard state
    94.     SetKeyboardState kbArray
    95. End Sub
    96. Private Sub TurnOff(vkKey As Long)
    97.     'Get the keyboard state
    98.     GetKeyboardState kbArray
    99.     'change a key
    100.     kbArray.kbByte(vkKey) = 0
    101.     'set the keyboard state
    102.     SetKeyboardState kbArray
    103. End Sub
    104. Private Sub Form_Unload(Cancel As Integer)
    105.     'restore the old keyboard state
    106.     SetKeyboardState kbOld
    107. End Sub
    Last edited by Pirate; Feb 24th, 2004 at 01:42 AM.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm thanks...
    I didnt know about the GetKeyState api, I thought I had only the option of calling GetKeyBoardState which makes it a bit more complicated.. anyways I have these two definitions. The first one works, but the second one doesn't. I don't understand why

    VB Code:
    1. Private Const VK_CONTROL As Integer = 17
    2.     Private Const VK_SHIFT As Integer = 16
    3.  
    4.  
    5.     ' works
    6.     Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Integer
    7.  
    8.     ' raises this error: [b]System.InvalidProgramException:PInvoke item (field,method) must be Static.[/b]
    9.     <DllImport("user32")> _
    10.     Private Function GetKeyState(ByVal nVirtKey As Integer) As Integer
    11.     End Function

    anyways, it seems like it returns something positive if the key is up and negative if its down

    edit: eeh returns 0 or 1 if the key is up, I think.
    Last edited by MrPolite; Feb 23rd, 2004 at 09:08 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite
    ' works
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Integer

    ' raises this error: System.InvalidProgramException:PInvoke item (field,method) must be Static.
    <DllImport("user32")> _
    Private Function GetKeyState(ByVal nVirtKey As Integer) As Integer
    End Function
    [/Highlight]
    <DllImport> attribute is not required for declaring APIs in VB.NET .

    The error raising in the second declaration is mistyped I think . You forgot 'Declare' keyword .

    An example for GetKeyState :

    VB Code:
    1. 'In a module
    2. Public Const WH_KEYBOARD = 2
    3. Public Const VK_SHIFT = &H10
    4. Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    6. Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    7. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    8. Public hHook As Long
    9. Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    10.     'if idHook is less than zero, no further processing is required
    11.     If idHook < 0 Then
    12.         'call the next hook
    13.         KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    14.     Else
    15.         'check if SHIFT-S is pressed
    16.         If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
    17.             'show the result
    18.             Form1.Print "Shift-S pressed ..."
    19.         End If
    20.         'call the next hook
    21.         KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    22.     End If
    23. End Function
    24.  
    25. 'In a form, called Form1
    26. Private Sub Form_Load()
    27.     'KPD-Team 2000
    28.     'URL: [url]http://www.allapi.net/[/url]
    29.     'E-Mail: [email][email protected][/email]
    30.     'set a keyboard hook
    31.     hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
    32. End Sub
    33. Private Sub Form_Unload(Cancel As Integer)
    34.     'remove the windows-hook
    35.     UnhookWindowsHookEx hHook
    36. End Sub
    37.  
    38. Parameter info :
    39.  
    40. · nVirtKey
    41. Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.
    42. If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the “o” key, whereas VK_OEM_1 refers to the “o with umlaut” key.
    43.  
    44.  
    45. Returned Value :
    46.  
    47. If the function succeeds, the return value specifies the status of the given virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key’s indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.

    And you may need to look up the API 'GetAsyncKeyState' .

  9. #9
    New Member
    Join Date
    Jun 2011
    Posts
    1

    Re: how can I find out if the control key is down?

    Better late than never? (VB.Net Express 2010)

    if My.Computer.Keyboard.CtrlKeyDown then
    ' Do Something
    end if

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