r/visualbasic • u/mudderfudden • Jan 01 '24
Need Help Understanding Keypress Events
EDIT 1/1/24: I added a texbox (sent to back under a WebView2 control), made sure it had focus on launch (txtTextbox1=Select() in frmMyForm_Load (required, or it didn't work) and I was able to get keypresses to respond and show a message:
If Asc(e.KeyChar) = 97 Or Asc(e.KeyChar) = 65 Then
MsgBox("Screen A")
ElseIf Asc(e.KeyChar) = 98 Or Asc(e.KeyChar) = 66 Then
MsgBox("Screen B")
ElseIf Asc(e.KeyChar) = 99 Or Asc(e.KeyChar) = 67 Then
MsgBox("Screen C")
ElseIf Asc(e.KeyChar) = 100 Or Asc(e.KeyChar) = 68 Then
MsgBox("Screen D")
End If
I'm attempting to resize and reposition the WebView2 control when a toggle is pressed, but nothing is happening. For instance, I'll do this:
If Asc(e.KeyChar) = 97 Or Asc(e.KeyChar) = 65 Then
wvScreenA.Left = 0
wvScreenA.Top = 0
wvScreenA.Width = 1920
wvScreenA.Height = 1080
End If
*** Original Post Below***
I had the idea to create a form, full screen on my monitor, so that's 1920x1080, I placed four WebView2 controls on it, each loading a separate website. So, I have:
- frmMyMainForm
- wvScreenA
- wvScreenB
- wvScreenC
- wvScreenD
Each WebView2 control does in fact load its own website correctly and scales it down.
I had this idea to use the corresponding letter as a Full Screen toggle for each WebView2 control. That is, if I pressed A, B, C or D (lowercase or capital), the corresponding WebView2 control would go full screen or return to its normal state and position.
I can't seem to understand how Keypress Events are coded and if I need to load the event functions or what. Can someone explain?
I also probably need to know if I need to use the Keypress event of my form or individual WebView2 controls or what?
View all comments
Show parent comment
1
u/mudderfudden Jan 04 '24
I think after some tinkering around, I can get it working. My code is long and I'm sure there's a better way to do this but I'd like to do it the long way first before I start making it shorter.
What I'm most concerned about right now, is when I click inside of a WebView2 control, my text box loses focus, thus rendering pressing A, B, C, or D, useless. Adding in a text box and placing it beneath one of the WebView2 controls was the only way I could think of to get this semi-working with the key presses.
I'm not sure how to trap clicking on a link within a WebView2 control to force it to focus on my textbox. Any ideas?