PDA

Click to See Complete Forum and Search --> : [RESOLVED] Detecting Shift and Control Keypress in VS2005 and CF20


easymoney
Aug 19th, 2007, 10:44 PM
I am coding an app using Jett Xl handheld PC running Windows CE .NET 5.0. It has a full keypad that I am attempting to detect key combinations such as Control C (^C), etc.

The following code will detect a KeyPress such as the letter "O":

Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim chrKeyPress As Char

chrKeyPress = e.KeyChar.ToString.ToUpper

If chrKeyPress = "O" Then
frmOptions.Show()
e.Handled = True
End If

I tried something like:

If e.shift Then Then
MsgBox("Shift")
End If

No luck, that must be older .Net, does not seem to be supported. Is there another method that is supported in VS 2005 and CF20?

petevick
Aug 20th, 2007, 01:00 AM
Hi,
try using the keydown event rather than the keypress event - then you can do e.control

Pete

easymoney
Aug 23rd, 2007, 03:02 AM
That is the event that does the trick.... thanks.

Private Sub frmAccount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.Control.ToString And e.KeyCode = Keys.H Then
TabControl1.SelectedIndex = 1
e.Handled = True
End If

If e.Control.ToString And e.KeyCode = Keys.E Then
TabControl1.SelectedIndex = 0
e.Handled = True
End If

End Sub

On my form I have a textbox that hold some data that selected text. When I am on SelectedIndex = 0 and I press ^E, the selected text remains selected and unchanged. When I then press ^H, the SelectedIndex is changed to 1, but the textbox value is deleted.

For example, if Textbox1 has the number 123456 and it is all highlight, and I press ^H, the data is deleted from Textbox1 and then the Tab is changed to Tab1 is selected.

Why is the keypress deleting my text? If the focus is set to Textbox1 and nothing is highlighted, it just deletes on digit from the Textbox1 when press ^H.

Any clue to how I can surpess the keypress that is changing the value of Textbox1?

Confused :blush:

easymoney
Aug 23rd, 2007, 03:11 AM
It has nothing to do with the tab control, I used this mod to test the keydown event handling:

If e.Control.ToString And e.KeyCode = Keys.H Then
TabControl1.SelectedIndex = 0
e.Handled = True
End If

If e.Control.ToString And e.KeyCode = Keys.E Then
TabControl1.SelectedIndex = 0
e.Handled = True
End If

When Textbox1 has focus, ^E is handled and text is unchanged. When I press ^H, it seem to get handled, but it seems to do a backspace as well deleted any highlighted text, or erases one char in front of cursor, ie a backspace.

How can I stop ^H from affecting text in Textbox1?

easymoney
Aug 23rd, 2007, 03:30 AM
Ok, ^H seems to do backspace in any textbox.

How come e.handled does not strip the backspace function from ^H? Is that not what it should do?

easymoney
Aug 23rd, 2007, 03:48 AM
Ok, this seems to work. I made a Public varible, the set it in the KeyDown event and then used e.Handled in the KeyPress event.

This method, it seems that e.handled performs as expected.

Private Sub frmAccount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

bControl = False

If e.Control.ToString And e.KeyCode = Keys.H Then
TabControl1.SelectedIndex = 1
bControl = True
End If

If e.Control.ToString And e.KeyCode = Keys.E Then
TabControl1.SelectedIndex = 0
bControl = True
End If

End Sub

Private Sub frmAccount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

If bControl = True Then
e.Handled = True
End If

End Sub

Is this a correct implementation...?

petevick
Aug 23rd, 2007, 04:09 AM
Hi,
looks good to me

Thanks

Pete

easymoney
Aug 26th, 2007, 02:00 AM
Thanks. I will mark resolved.