1

I want to trigger code were if i press ctrl + G on the keyboard, it will trigger my code! Thing is, it wont work. If i hit the letter u or g on its own, my else triggers grand! What am i doing wrong?

My question is different than others because im trying to get a keypress event off of a webbrowser component. So i have to make do with what i got and make a keypress event using document.body.keydown adding that into a eventhandler

My code:

Private Sub AdsDisplayer_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles AdsDisplayer.DocumentCompleted
    AddHandler AdsDisplayer.Document.Body.KeyDown, AddressOf AdsDisplayer_KeyDown
End Sub

Private Sub AdsDisplayer_KeyDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    If e.CtrlKeyPressed And Chr(e.KeyPressedCode) = "G" Then
        'CTRL+G was pressed.
        MsgBox("CTRL + " & Chr(e.KeyPressedCode))
    Else
        'SomeKey was pressed without any modifier keys.
        MsgBox(Chr(e.KeyPressedCode))
    End If
End Sub

EDIT

What i tried from below comments and not working:

If (e.KeyPressedCode And Not Keys.Modifiers) = Keys.G AndAlso e.CtrlKeyPressed = Keys.ControlKey Then
    MsgBox("Ctrl + G")
End If
William
  • 1,009
  • 15
  • 41
  • 1
    Possible duplicate of [How do you detect simultaneous keypresses such as "Ctrl + T" in VB.NET?](https://stackoverflow.com/questions/13803761/how-do-you-detect-simultaneous-keypresses-such-as-ctrl-t-in-vb-net) and also of: [VB.Net Key Combination](https://stackoverflow.com/questions/4442805/vb-net-key-combination) – Mederic Jun 07 '17 at 09:36
  • Please remember to use the search function before asking a question. – Mederic Jun 07 '17 at 09:36
  • I did, im using webbrowser component not a normal keypress. Please look into the question before stating. I get, keypress is not a member error. Since im using `HtmlElementEventArgs` – William Jun 07 '17 at 09:38
  • This is not a duplicate.. The above does NOT work for my question. – William Jun 07 '17 at 09:42
  • Did you try AndAlso as suggested in my links? – Mederic Jun 07 '17 at 09:42
  • I sure did! Jumps straight into my else. – William Jun 07 '17 at 09:43
  • I even tried: `If (e.KeyPressedCode And Not Keys.Modifiers) = Keys.G AndAlso e.CtrlKeyPressed = Keys.ControlKey Then MessageBox.Show("Ctrl + G") End If` – William Jun 07 '17 at 09:48
  • KeyPressedCode uses integer you can try to use: `If e.CtrlKeyPressed And e.KeyPressedCode = CType(Keys.G,Integer) Then` – Mederic Jun 07 '17 at 09:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146043/discussion-between-irishwill200-and-mederic). – William Jun 07 '17 at 09:53

2 Answers2

2

KeyDown vs KeyUp

The problem here comes from the timing of the event you use. You use the KeyDown event and this means it is fired as soon as Ctrl is down.


Using KeyUp

KeyUp will be able to solve this as it will detect when you have released one key.

Hence we would get:

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        AddHandler WebBrowser1.Document.Body.KeyUp, AddressOf WebBrowser1_KeyUp
End Sub

And then just get the Ctrl + G as follows:

Private Sub WebBrowser1_KeyUp(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    If e.CtrlKeyPressed And e.KeyPressedCode = CType(Keys.G, Integer) Then
        'CTRL+G was pressed.
        Console.WriteLine("Here is the Ctrl + G")
    Else
        'Wrong combo
    End If
End Sub
Mederic
  • 1,949
  • 4
  • 19
  • 36
  • This is what i used from the discussion, still no luck! Wont fire, if i press a key different it goes into the else ok. So it does fire, just wont fire the main if for `Ctrl + G` I even deleted my webbrowser component and added a new one so its named `WebBrowser1` and i did a direct copy, paste! Still no luck? :/ – William Jun 07 '17 at 10:05
  • I works for me so that's a bit weird. this solution works if you release the G first I think are you releasing ctrl first? – Mederic Jun 07 '17 at 10:06
  • Im releasing the G first. If i release the Ctrl first i get into the else. If i Hold Ctrl and press G nothing fires. Is this to do with my webbrowser component? I am using `Microsoft Visual Studio Ultimate 2012 Version 11.0.61219.00 Update 5 Microsoft .NET Framework Version 4.6.01055` – William Jun 07 '17 at 10:11
  • Make sure you have clicked on the Browser you need to be on browser focus it isn't by default when the page loads. – Mederic Jun 07 '17 at 10:13
  • Yeah im doing all of that. So i added another webbrowser component, pointed it to google.ie and the code works for that one? I think its to do with the site im pointing it to. This is very strange :/ – William Jun 07 '17 at 10:15
  • No idea for your website but for me on google.com it works. – Mederic Jun 07 '17 at 10:16
  • Same for me.. Must be website related.. No worries! Il look into this myself, the code works so im happy! Thanks very much for your time. – William Jun 07 '17 at 10:18
  • @irishwill200 no problem if this at least solved your key press error you can validate the answer. And you should start new question maybe for why some websites work and others no some people might be able help – Mederic Jun 07 '17 at 10:19
  • @irishwill200 no problem if this at least solved your key press error you can validate the answer. And you should start new question maybe for why some websites work and others no some people might be able help – Mederic Jun 07 '17 at 10:20
  • I noticed, if you click onto the webbrowser it seems to make the listener disable? As if i never touch the browser it works. If i click on the browser control (anywhere) then the code doesnt trigger? Why is this? Thanks! – William Jun 07 '17 at 12:39
1

General Information

While looking further into this question of mine i found the event Control.PreviewKeyDown located at: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx solves my question!

Since above (Mederic's Question) did work but only on initialize i had to go scouting for answers as to why, when i click inside the webbrowser component my listener stopped firing! My thoughts where that the form no longer had focus even though it was meant to be a listener for the browser.

My Aim?

My aim was to create a secret combination of such so that once pressed (It being anything like CTRL + A) then it would open a settings tab where the end user can input settings to their liking which the form can change to. Thing is, webbrowser component doesnt come with events such as KeyUp,MouseDown etc so you are left to find other ways! The example code i supplied in my question was a way i found in numerous websites but failed to please me. As stated above, i then came across the event called PreviewKeyDown which thankfully the webbrowser component could use!

How PreviewKeyDown Works For Me?

Coming back to the idea of a secret combination. I dont need anything special like keyup as this event fires exactly how i want it! By just putting the event in place and hooked to my webbrowser component i can just have a simple IF Statement which detects what i press and then that triggers i can then call my form.show(), me.hide() sequence.

For some reason, no one could simply tell me thats the event i needed to save my time looking but i guess im happy i found it in the end!

My Code

Private Sub WebBrowser1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown
    If e.Control AndAlso e.KeyCode = Keys.A Then 'If control and A keys are pressed
        Form.Show() 'Show new form
        Me.Hide() 'Hide Current form
    End If
End Sub

Thats it folks! Hope this will help someone like me in the future :)

Community
  • 1
  • 1
William
  • 1,009
  • 15
  • 41