Hey all,

I have a picture box, and I want to detect whether the ctrl key is being pressed.
Since I want to simulate... ctrl and left click for multiple selections.

what I have done:
1. set beingpressed as boolean
2. in keyDown event, I set beingpressed to true
3. in keyUp event, I set beingpressed to false

My problem is:
sometimes the keyUp event is invoked while the program is executing some other process. so the program cannot "catch" that keyUp. Then the program will assume that (ctrl key) beingpressed is always true until the rest of the time... which is dangerous... :(

Do you have any idea to work around this?
Sounds like concurrency problem that I have here...
How can somehow I check the ctrl key status (whether it is being pressed or not) without using keyUp and keyDown event?

thanks

Have a look at THIS post with an attached sample on using the mouse click event with shift or ctrl.

The code looks something like -

Private Declare Sub keybd_event Lib "USER32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function SetCursorPos Lib "USER32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "USER32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000   ' absolute move
Const KEYEVENTF_KEYUP = &H2
Const VK_LWIN = &H5B
Public dest_x As Integer
Public dest_y As Integer
Private Sub Command1_Click()
SingClick 100, 100 'send a click to pixel (100,100)
Call keybd_event(16, 0, 0, 0) 'Send down key press to Key 16 (16 is Shift 17 is ctrl)
SingClick 100, 400 'send a click to pixel (100,100)
Call keybd_event(16, 0, KEYEVENTF_KEYUP, 0) 'release Shift key
End Sub
Public Sub SingClick(dest_x As Integer, dest_y As Integer)
Dim ptx As Integer
Dim pty As Integer
zc = Form1.ScaleX(Screen.Width, vbTwips, vbPixels)
ptx = dest_x * 65535 / Form1.ScaleX(Screen.Width, vbTwips, vbPixels)
pty = dest_y * 65535 / Form1.ScaleX(Screen.Height, vbTwips, vbPixels)
    
    mouse_event _
            MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, ptx, pty, 0, 0
    DoEvents
    mouse_event _
        MOUSEEVENTF_ABSOLUTE + _
        MOUSEEVENTF_MOVE + _
        MOUSEEVENTF_LEFTDOWN + _
        MOUSEEVENTF_LEFTUP, _
        ptx, pty, 0, 0
Sleep 30: DoEvents
End Sub
Private Sub Form_Load()
Me.Left = Screen.Width - Me.Width ' moves the form out of the way
End Sub

Thanks a lot mate, it works :)

I call "Call keybd_event(17, 0, KEYEVENTF_KEYUP, 0)" in the mouseup event, and it works miraculously :)

Only a pleasure.:)

Happy coding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.