How to detect Ctrl kkey Down ?

vmars316 621 Reputation points
2020-09-21T21:18:33.507+00:00

Hello & Thanks ,
win 10 vs 2019 vb.net .vb WebBrowser app .
I am trying to detect Ctrl key pressed .
For some sites , in this case pbskids.org ,
I am unable to make a click on image-link work .
But in 'Sub TimerStatus_Tick' hover over link , shows in StatusLbl control .
So what I want to do is when StatusLbl shows a link , if I click on Ctrl-key ,
then navigate to that site .
Pls , how to get If e.KeyCode = Keys.Control work ?

    Public Sub SafeBrowser_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Control Then
            If StatusLbl.Text.Length > 6 Then
                WebBrowser1.Navigate(StatusLbl.Text)
            End If
        End If
    End Sub

    Private Sub TimerStatus_Tick(sender As Object, e As EventArgs) Handles TimerStatus.Tick
        StatusLbl.Text = WebBrowser1.StatusText.ToString
        '        txbAddress.Text = WebBrowser1.StatusText.ToString
    End Sub

Thanks for your Help...

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2020-09-22T02:13:55.983+00:00

    Hi Vernon Marsden,
    Using .NET 4, you can use ModifierKeys.HasFlag method.
    I made a test in Form_keyDown event you can refer to.

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)  
        If ModifierKeys.HasFlag(Keys.Control) Then  
            '  MessageBox.Show("Ctrl is pressed!")  
        End If  
    End Sub  
    

    If you're not using .NET 4, the availability of Enum.HasFlag is not applicable.
    You can use following code to achieve the same result:

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)  
        If (Control.ModifierKeys And Keys.Control) = Keys.Control Then  
            '  MessageBox.Show("Ctrl is pressed!")  
        End If  
    End Sub  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. vmars316 621 Reputation points
    2020-09-22T20:16:53.267+00:00

    Thanks Daniel ;
    I think we are close , but get this error;
    1>C:\Users\vmars\source\repos\SafeBrowser\Form1.vb(16,12): error BC30020: 'Is' operator does not accept operands of type 'Keys'. Operands must be reference or nullable types.
    1>C:\Users\vmars\source\repos\SafeBrowser\Form1.vb(16,55): error BC30020: 'Is' operator does not accept operands of type 'Keys'. Operands must be reference or nullable types.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    For this line;
    If (Control.ModifierKeys And Keys.Control) Is Keys.Control Then

    How do can this be fixed ?
    Thanks for your Help...


  3. vmars316 621 Reputation points
    2020-09-23T16:35:38.417+00:00

    Hi Daniel ;
    Ugh !
    Still not working:
    I tried it on WebBrowser control and Form control .
    I must be doing something wrong :

        Private Sub WebBrowser1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If (Control.ModifierKeys And Keys.Control) = Keys.Control Then
                '                    (CType(sender, Control)).Hide()
                '            If StatusLbl.Text.Length > 6 Then
                MsgBox("WebBrowser1 =  " & StatusLbl.Text)
                WebBrowser1.Navigate(StatusLbl.Text)
                '            End If
            End If
        End Sub
        Private Sub SafeBrowser_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If (Control.ModifierKeys And Keys.Control) = Keys.Control Then
                '                    (CType(sender, Control)).Hide()
                '            If StatusLbl.Text.Length > 6 Then
                MsgBox("SafeBrowser =  " & StatusLbl.Text)
                WebBrowser1.Navigate(StatusLbl.Text)
                '            End If
            End If
        End Sub
    

    Thanks