Results 1 to 8 of 8

Thread: [RESOLVED] Detecting Shift and Control Keypress in VS2005 and CF20

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Resolved [RESOLVED] Detecting Shift and Control Keypress in VS2005 and CF20

    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":

    Code:
        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:

    Code:
            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?

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    Hi,
    try using the keydown event rather than the keypress event - then you can do e.control

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    That is the event that does the trick.... thanks.

    Code:
        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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    It has nothing to do with the tab control, I used this mod to test the keydown event handling:

    Code:
            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?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    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?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    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.

    Code:
        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...?

  7. #7
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    Hi,
    looks good to me

    Thanks

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    352

    Re: Detecting Shift and Control Keypress in VS2005 and CF20

    Thanks. I will mark resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width