Results 1 to 8 of 8

Thread: Left or Right Ctrl Button?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    188
    Is there a way to know if the user pressed the left or the right ctrl key on their keyboard. I need to be able to do different things depending on the ctrl left and ctrl right keys being pressed.

  2. #2
    Guest
    Try this method. It uses DirectInput so you must include the Type Library. If you do not have the Library, I can send it to you.

    Make a Form with a Timer called Timer1 and Insert the below code into the Form.

    Code:
    Dim dx7 As New DirectX7
    Dim state As DIKEYBOARDSTATE
    Dim iKey As Integer
    Dim dinput As DirectInput
    Dim keys(255) As String
    Dim dev As DirectInputDevice
    
    Private Sub Form_Load()
        Timer1.Interval = 50
        Set dinput = dx7.DirectInputCreate()
        Set dev = dinput.CreateDevice("GUID_SysKeyboard")
        dev.SetCommonDataFormat DIFORMAT_KEYBOARD
        dev.Acquire
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        dev.Unacquire
    End Sub
    
    Private Sub Timer1_Timer()
    dev.GetDeviceStateKeyboard state
    For iKey = 0 To 255
        If state.Key(iKey) <> 0 Then
        End If
    Next
    
    If state.Key(29) <> 0 Then Print "Left CTRL"
    If state.Key(157) <> 0 Then Print "Right CTRL"
    DoEvents
    
    End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    188
    Thanx!

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Another way to do it

    Here is another way you can do it too.

    Code:
    Option Explicit
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Private Const VK_LCONTROL = &HA2
    Private Const VK_LSHIFT = &HA0
    Private Const VK_RCONTROL = &HA3
    Private Const VK_RSHIFT = &HA1
    
    Private Sub Form_Load()
    Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
    If GetKeyState(VK_LCONTROL) < 0 Then
        MsgBox "Left Control Key is press"
    ElseIf GetKeyState(VK_RCONTROL) < 0 Then
        MsgBox "Right Control Key is press"
    ElseIf GetKeyState(VK_LSHIFT) < 0 Then
        MsgBox "Left Shift Key is press"
    ElseIf GetKeyState(VK_RSHIFT) < 0 Then
        MsgBox "Right Shift Key is press"
    End If
    End Sub
    [Edited by Chris on 06-22-2000 at 10:50 PM]

  5. #5
    Guest
    Using Chris's method is better because you do not need to include the Type Library or initialize any events.

    However, the DirectInput can access 255 different keys which is better for gaming.


  6. #6
    Member
    Join Date
    Jul 2000
    Location
    Sydney, Nova Scotia
    Posts
    55
    Can u make this run from an executable that doesn't have the focus and a direct-x game is running on top of it?
    Vince McMullin
    Aka Vam, Vinny Mac

    Its just that simple

  7. #7
    Guest
    Yes, the DirectInput method will work if your App doesn't have the focus. Chris's method will only work for Windows NT so I cannot test that out, but I'm pretty sure it will work as well.

  8. #8
    Member
    Join Date
    Jul 2000
    Location
    Sydney, Nova Scotia
    Posts
    55
    When the game is loaded it won't work, I'm guessing the game is resetting the dinput access.

    But there is a cheat program out for the game that uses Hotkeys and it works fine, so I'm really baffeled by this.

    The code works with any other application running on top of my program except for this game.





    Could the cheat program have some Dinput hacks for this game? Or is there an actual way to do this?



    I spent hours coding message stuff thinking this hotkey issue wasn't that big but now I find it is......... ?
    Vince McMullin
    Aka Vam, Vinny Mac

    Its just that simple

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