Campaign for KeyDown

R

Richard Kucia

I'm trying to intercept the arrow and return keystrokes with the KeyDown
event. Yes, I've tried the McSoft package and it won't work for me.

I believe a TextBox and only one or two other controls make the KeyDown
event available. For my testing, added a handler to a textbox's KeyDown
event. Upon each event, I added the KeyCode to a combo box. I found some
interesting results which I am happy to share here. In all tests, I set the
focus to the textbox and pressed the Up, Down, Left, Right and Return keys
in order. The test results show the KeyCode.ToString results generated from
each physical key pressed. Note that in some cases, one human action
produced two events (i.e. two keypresses were generated).

Test 1: Emulator; clicking the star pad with the mouse
Up, F21
Down, F21
Left, F21
Right, F21
Return

Test 2: Emulator, clicking the arrow keys on the SIP
Up
Down
Left
Right
Return

Test 3: Emulator, pressing keys on the host computer
Up, F20
Down, F20
Left, F20
Right, F20
Return

Test 4: Compaq iPaq, tapping keys on the SIP
Up
Down
Left
Right
Return and a beep

Test 5: Compaq iPaq, pressing the star pad
Up, F21
Down, F21
Left, F21
Right, F21
F23, Return and a beep

And that leads to a few questions:

1) What's with the the F20, F21 and F23 keycodes? What do they signify? It
would make some sense if the F## keystroke always preceded the usual keycode
(so the software could perhaps handle the star pad differently from the
SIP), but it's a real kludge if the F## keystroke follows the usual keycode.

2) Can I get rid of the beep? I tried setting AcceptsReturn both ways on the
textbox and that didn't seem to affect the beep.

3) What other controls support KeyDown detection?

4) Design question: I've got a tab control with 4 tab pages. The tab page of
interest to me has a bunch of buttons, combo boxes, trackbars, etc. There's
also a bunch of combo boxes and list views that are outside the entire tab
control. I don't need the SIP for any of these controls. I'd like to use the
Up/Down/Return keys from the star pad. What do you think about this
approach:

* deriving a class ("MyGizmo") from one of the controls that supports
KeyDown (probably not the TextBox since it beeps for Return)
* instantiating Gizmo on the tab page of interest
* setting the Gizmo invisible (or, if it must be visible in order to get
focus, then creating a 1-pixel screen representation)
* adding a handler to all the other screen controls' GotFocus event to send
the focus over to the Gizmo control
* adding a handler to the Gizmo KeyDown event; if the tab control's selected
tab page is the one I'm interested in, and the keycode is one of my special
keycodes, then do whatever needs to be done and mark the keycode as handled

Boy, I sure wish I had a form-level KeyPreview....

Thanks.

Richard Kucia
 
D

Drew

Thanks for posting this.

Being a newbie, I was starting to go crazy not understand why I'm not
getting any KeyDown events off my control.

I need to know what components do support KeyDown, please.

Sorry I can't answer your questions.

Drew
 
R

Richard Kucia

In my attempt to grab the KeyDown event for the star pad, I created a custom
control as shown below. My intention is to create a control that forever
keeps the focus and thus can process all keys. This design only has a chance
of working because I don't have any other controls on this particular form
that require the keyboard/SIP. The form has 4 buttons and 2 trackbars in it.
I don't care if I have to give up keyboard adjustments to the trackbars.

The code indeed intercepts the KeyDown event, but sometimes it goes into an
infinite loop (not a recursive loop -- an infinite loop). If I have a
breakpoint inside the OnKeyDown routine, and I press the Left key, then
OnKeyDown seems to receive a continuous stream of Left keys (i.e. not just
one). Pressing another key sometimes alters the behavior -- I've removed the
breakpoint and pressed "B". The routine then receives a stream of "B"s.
However, sometimes pressing keys works just fine. I suspect the problem is
due to the first occurrence of a "special" keystroke.

What am I doing wrong?

By the way, I found it very interesting that an object can have the focus
and respond to key events even though it isn't visible and is not
incorporated into any visible control's collection.

Thanks.
Richard Kucia

Hey Microsoft! Please incorporate a form-level KeyPreview in the next
release!



Private Class Gizmo
Inherits Control

' Some other control [sender] just received the focus; rip it away

Private Sub SnatchFocusHandler(ByVal sender As Object, ByVal e As EventArgs)
Me.Focus()
End Sub

Public Sub SnatchFocus(ByVal ParamArray ControlList() As Control)
For Each ctl As Control In ControlList
AddHandler ctl.GotFocus, AddressOf Me.SnatchFocusHandler
Next
End Sub

Public Shadows Event KeyDown(ByVal sender As Object, ByVal k As
KeyEventArgs)

Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
Dim S As String
S = e.KeyCode.ToString & " (" & e.KeyData.ToString & ")"
e.Handled = True
Select Case e.KeyCode
Case Keys.Up, Keys.Down, Keys.Left, Keys.Right, Keys.Return
RaiseEvent KeyDown(Me, e)
End Select
End Sub

End Class

Dim WithEvents gizTest As Gizmo

Private Sub GizmoKeyDownHandler(ByVal sender As Object, ByVal k As
KeyEventArgs)
Dim S As String, I As Integer
S = sender.GetType.ToString
S = Mid(S, S.LastIndexOf("+") + 2)
S = S & ": " & k.KeyCode.ToString & " (" & k.KeyData.ToString & ")"
cboDebug.Items.Add(S)
cboDebug.Visible = True
cboDebug.BringToFront()
End Sub

Private Sub TestTheGizmo()
gizTest = New Gizmo
AddHandler gizTest.KeyDown, AddressOf GizmoKeyDownHandler
gizTest.SnatchFocus( _
btnCmdJogCCW, _
btnCmdJogCW, _
btnCmdJogTeach, _
btnCmdJogHalt, _
tkbCmdJogDistance, _
tkbCmdJogVelocity)
End Sub
 
A

Armin Sadeghi [MSFT]

The only controls that support key events for RTM and SP1 release of NetCF
are: Form, Control (and thus all derived custom controls), and TextBox.
Other controls do not support key events.


-Armin

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Drew" <[email protected]>
| References: <[email protected]>
| Subject: Re: Campaign for KeyDown
| Date: Tue, 12 Aug 2003 13:44:15 -0600
| Lines: 13
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: dnvr185.dslgw1poolj1.dnvr.uswest.net 65.103.110.186
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:30764
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Thanks for posting this.
|
| Being a newbie, I was starting to go crazy not understand why I'm not
| getting any KeyDown events off my control.
|
| I need to know what components do support KeyDown, please.
|
| Sorry I can't answer your questions.
|
| Drew
|
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top