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.
Printable View
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.
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
Thanx!
Here is another way you can do it too.
[Edited by Chris on 06-22-2000 at 10:50 PM]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
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.
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?
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.
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......... ?