0

I'm creating an windows form application. I have to do some actions after some of keyboard shortcuts is pressed. I have this part :

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    If keyData = Keys.Control & Keys.T Then
        'Wait till mouse clicks one of application controls
        'And get selected control information
    Else
        MyBase.ProcessCmdKey(msg, keyData)
    End If
End Function

Also, after this keyboard keys combination is pressed I have to get a control which is selected by mouse click. I am not able to make an event on every main form control (well, even if it would be possible I believe it's a stupid choice).

One of solution I have is Me.ActiveControl but it can't focus on labels and some other controls which can't be focused. So it's perhaps not the right choice.

Another solution which I found is global hooks but I can't understood how to use it. I'm asking your help and advices. If you have sample of global hooks or better solution how to get mouse clicked control share it :) Any help is appreciated, feel free to write your opinion :)

intentarr
  • 197
  • 3
  • 12

1 Answers1

0

Your current approach is backwards for Windows programming.

The correct approach is to check in the control's click event if Ctrl+T is being pressed and respond accordingly.

  1. Import the Win API 'GetKeyState'
  2. Use 'GetKeyState' to check if the Ctrl Key and the T key are down.

See the PInvoke page for more information.

The correct way to handle a click event for all controls is simply to add a central click event handler for all controls placing your key down logic in that handler.

These are advanced techniques which require more in depth knowledge of WinForms.

Community
  • 1
  • 1