• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1604
  • Last Modified:

With the WebBrowser control, how can I capture keypress?

I want to utilize the WebBrowser control as an entry point so that I can do data validation. Is there a method where I can capture the keypress?

Thanks for any help here.

Regards,
TJ
0
TJenner2
Asked:
TJenner2
  • 5
  • 2
  • 2
1 Solution
 
List244Commented:
On your form which contains the WebBrowser control, set the propery KeyPreview to true.  This will
allow all keys in your application to be sent to your form key events.  This way even if they are in the
browser, you can get the keys they press.
0
 
List244Commented:
Also, to continue with this method, you can have some sort of boolen which you use like so:

Private IB as Boolean
Private Sub WebBrowser1_GotFocus()
IB = True
End Sub

Private Sub WebBrowser1_LostFocus()
IB = False
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (IB = True) Then
   ''CODE
End If
End Sub

Something like this code will allow you to say IF the webbrowser has focus... then you can
manage the keys pressed inside of it as needed.
0
 
daniel_j_cCommented:
you can also hook all the events inside the DOM (document object model) of the browser like this:

Option Explicit

'you need to add a reference to 'microsoft html object library' to get this defined
Dim WithEvents oDoc       As HTMLDocument

Private Sub Form_Load()

 WebBrowser1.Navigate2 "www.google.com"
 
End Sub

Private Sub oDoc_onkeydown()

  Debug.Print oDoc.parentWindow.event.keyCode & " pressed"
 
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

 'you can't hook the dom before this because a fresh one is created when
 'you browse to a new url
 Set oDoc = WebBrowser1.Document
End Sub

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TJenner2Author Commented:
I am getting a variable not defined on:
    Set oDoc = wWeb.Document

wWeb is my WebBrowser1

Now I have used:
   DIM oDoc as HTMLDocument
   DIM oDoc as DOMChildrenCollection
   DIM oDoc

and have not got any result in the Immediate Windows.

What next?

TJ
0
 
List244Commented:
TJenner2, if you follow the first two comments I provided, the code, and the keypreview, you will get
captured keys from your web browser control.
0
 
TJenner2Author Commented:
I followed your code, and it does work, just not as implied.

 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Gives the impression that KeyCode would be the ascii code of the character typed, but when used it produces the UCASE of the character regardless if the caps lock or shift is used. This was proven by using this line:

Debug.Print Chr(KeyCode);

If it wasn't for this flaw, I would except your answer as a solution, but when doing Data Validation, I need to validate proper data and your solution does not do that.

Regards,
TJ
0
 
List244Commented:
Actually, the keycode and ascii code are different, if you want to get the ascii-code of a keycode, you can use
something like:

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Function ShiftKey(ShiftState As Integer) As Boolean
If GetKeyState(20) = 0 Then
    If ShiftState = 1 Then
        ShiftKey = True
    Else
        ShiftKey = False
    End If
Else
    If ShiftState = 1 Then
        ShiftKey = False
    Else
        ShiftKey = True
    End If
End If
End Function

Private Function TrueKey(Keycode As Integer, Shift As Integer) As Integer
TrueKey = 0 ''Non-printable char if you want to track change
'to truekey=keycode
If Keycode >= 65 And Keycode <= 90 Then 'a-z
    If ShiftKey(Shift) = False Then
        TrueKey = Keycode + 32
    Else
        TrueKey = Keycode
    End If
End If
If Keycode = 32 Then TrueKey = Asc(" ")
If Shift = 1 Then
    Select Case Keycode
        Case 48: TrueKey = Asc(")")
        Case 49: TrueKey = Asc("!")
        Case 50: TrueKey = Asc("@")
        Case 51: TrueKey = Asc("#")
        Case 52: TrueKey = Asc("$")
        Case 53: TrueKey = Asc("%")
        Case 54: TrueKey = Asc("^")
        Case 55: TrueKey = Asc("&")
        Case 56: TrueKey = Asc("*")
        Case 57: TrueKey = Asc("(")
        Case 189: TrueKey = Asc("_")
        Case 187: TrueKey = Asc("+")
        Case 219: TrueKey = Asc("{")
        Case 221: TrueKey = Asc("}")
        Case 220: TrueKey = Asc("|")
        Case 186: TrueKey = Asc(":")
        Case 222: TrueKey = Asc("""")
        Case 188: TrueKey = Asc("<")
        Case 190: TrueKey = Asc(">")
        Case 191: TrueKey = Asc("?")
        Case 111: TrueKey = Asc("/")
        Case 106: TrueKey = Asc("*")
        Case 109: TrueKey = Asc("-")
        Case 107: TrueKey = Asc("+")
        Case 192: TrueKey = Asc("~")
    End Select
Else
    Select Case Keycode
        Case 48, 96: TrueKey = Asc("0")
        Case 49, 97: TrueKey = Asc("1")
        Case 50, 98: TrueKey = Asc("2")
        Case 51, 99: TrueKey = Asc("3")
        Case 52, 100: TrueKey = Asc("4")
        Case 53, 101: TrueKey = Asc("5")
        Case 54, 102: TrueKey = Asc("6")
        Case 55, 103: TrueKey = Asc("7")
        Case 56, 104: TrueKey = Asc("8")
        Case 57, 105: TrueKey = Asc("9")
        Case 189: TrueKey = Asc("-")
        Case 187: TrueKey = Asc("=")
        Case 219: TrueKey = Asc("[")
        Case 221: TrueKey = Asc("]")
        Case 220: TrueKey = Asc("\")
        Case 186: TrueKey = Asc(";")
        Case 222: TrueKey = Asc("'")
        Case 188: TrueKey = Asc(",")
        Case 190: TrueKey = Asc(".")
        Case 191: TrueKey = Asc("/")
        Case 111: TrueKey = Asc("/")
        Case 106: TrueKey = Asc("*")
        Case 109: TrueKey = Asc("-")
        Case 107: TrueKey = Asc("+")
        Case 192: TrueKey = Asc("`")
    End Select
End If
End Function

That will convert the keycode to the ascii code and handle the shift/capslock issue.  But if you can get Daniel's method working it would
clear up some of the bulkiness this code provides.
0
 
daniel_j_cCommented:
you need to add a reference to 'microsoft html object library' for the HTMLDocument object to be defined.  You won't be able to get events on oDoc unless you can dim it as an HTMLDocument and use withevents.

If you want to use List244 method, why not just use the KeyPress event instead of the KeyDown event - the former returns an ascii code (which is your upper or lower case letter for example) while the latter returns the keyboard scan code.

I don't really care about the points so you can give them to List244 but hooking the DOM is a far more flexible solution and will only get you events from IE not from every keypress that happens on your form.
0
 
List244Commented:
Daniel, the problem with keypress is it wont run on the webbrowsercontrol.  So the only real method to do that is to use
KeyDown and get the keyascii from that.  However, TJenner, I actually would suggest as does Daniel, that you go with
his method if it works for you.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now