Results 1 to 3 of 3

Thread: [RESOLVED] WinForms, making a Hotkey TextBox (capturing Ctrl + Alt + Letter)

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Resolved [RESOLVED] WinForms, making a Hotkey TextBox (capturing Ctrl + Alt + Letter)

    I've got an application where I would like to add some global hotkey functionality to it, but I would like to allow the user change what the hotkey(s) are & Winamp has a global hotkey configuration where the user presses the optional keys (control, alt, shift) and the letter in a textbox and the textbox displays all of it, I would like to create a textbox that does the same thing, but I'm a little stuck.
    First off, here's a couple of screenshots showing what I'm aiming to do:
    Name:  WinampHotkeyTextBox1.png
Views: 555
Size:  55.7 KB Name:  WinampHotkeyTextBox2.png
Views: 541
Size:  50.9 KB Name:  WinampHotkeyTextBox3.png
Views: 508
Size:  55.0 KB Name:  WinampHotkeyTextBox.png
Views: 530
Size:  42.2 KB
    Here's what I have so far:
    vb Code:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Public Class HotKeyTextBox
    6.     Inherits System.Windows.Forms.TextBox
    7.  
    8. #Region " Variables "
    9.  
    10.     Private m_IsCtrl, m_IsAlt, m_IsShift As Boolean
    11.     Private m_Char As Char = CChar(String.Empty)
    12.  
    13. #End Region
    14. #Region " Sub: New "
    15.  
    16.     Public Sub New()
    17.         MyBase.New()
    18.         Me.ReadOnly = True
    19.         Me.ForeColor = SystemColors.Window
    20.  
    21.         m_IsCtrl = False
    22.         m_IsAlt = False
    23.         m_IsShift = False
    24.     End Sub
    25.  
    26. #End Region
    27. #Region " Overrides Subs: OnKeyDown, OnKeyUp "
    28.  
    29.     Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
    30.         MyBase.OnKeyDown(e)
    31.  
    32.         If e.Alt Then m_IsAlt = True
    33.         If e.Control Then m_IsCtrl = True
    34.         If e.Shift Then m_IsShift = True
    35.  
    36.         Call UpdateText()
    37.     End Sub
    38.  
    39.     Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
    40.         MyBase.OnKeyPress(e)
    41.  
    42.         If Char.IsLetterOrDigit(e.KeyChar) Then
    43.             m_Char = CChar(e.KeyChar.ToString.ToUpper)
    44.             Call UpdateText()
    45.         End If
    46.     End Sub
    47.  
    48.     Protected Overrides Sub OnKeyUp(e As System.Windows.Forms.KeyEventArgs)
    49.         MyBase.OnKeyDown(e)
    50.         If m_Char <> CChar(String.Empty) Then
    51.             If e.Alt Then m_IsAlt = False
    52.             If e.Control Then m_IsCtrl = False
    53.             If e.Shift Then m_IsShift = False
    54.  
    55.             Call UpdateText()
    56.         Else
    57.             m_IsAlt = False
    58.             m_IsCtrl = False
    59.             m_IsShift = False
    60.         End If
    61.     End Sub
    62.  
    63.     Private Sub UpdateText()
    64.         If (m_IsCtrl OrElse m_IsAlt OrElse m_IsShift) Then
    65.             Dim str As String = String.Empty
    66.             If m_IsCtrl Then str = "Ctrl"
    67.             If m_IsShift Then
    68.                 If m_IsCtrl Then str &= " + Shift" Else str = "Shift"
    69.             End If
    70.             If m_IsAlt Then
    71.                 If (m_IsCtrl OrElse m_IsShift) Then str &= " + Alt" Else str = "Alt"
    72.             End If
    73.  
    74.             If m_Char <> CChar(String.Empty) Then str &= " + " & m_Char
    75.  
    76.             Me.Text = str
    77.         Else
    78.             Me.Text = String.Empty
    79.             m_Char = CChar(String.Empty)
    80.         End If
    81.     End Sub
    82.  
    83. #End Region
    84.  
    85. End Class
    It's displaying the Ctrl + Alt + Shift combinations as I press them just fine, but when I press a letter (or number, I want to allow a single digit to be used too) it's not displaying it with the character at the end, I've got to be missing something simple and obvious, but right now it alludes me.
    Attached Files Attached Files
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: WinForms, making a Hotkey TextBox (capturing Ctrl + Alt + Letter)

    You'll need to do a bit more but here's a good start:
    vb.net Code:
    1. Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    2.     e.SuppressKeyPress = True
    3.  
    4.     Dim parts As New List(Of String)
    5.  
    6.     If e.Control Then
    7.         parts.Add("Ctrl")
    8.     End If
    9.  
    10.     If e.Shift Then
    11.         parts.Add("Shift")
    12.     End If
    13.  
    14.     If e.Alt Then
    15.         parts.Add("Alt")
    16.     End If
    17.  
    18.     Select Case e.KeyCode
    19.         Case Keys.ControlKey, Keys.ShiftKey, Keys.Menu
    20.             'Ignore modifier keys alone.
    21.         Case Else
    22.             parts.Add(e.KeyCode.ToString())
    23.     End Select
    24.  
    25.     Me.TextBox1.Text = String.Join(" + ", parts)
    26.  
    27. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: WinForms, making a Hotkey TextBox (capturing Ctrl + Alt + Letter)

    Thanks for your perspective jmc, it was a nudge in the right direction.
    I still had to use the KeyUp event to check if a modifier key had been released before a digit/letter key was pressed, want to visually show that to the user as it's happening.

    This seems to work the way that I would like it to:
    vb Code:
    1. Public Class HotKeyTextBox
    2.     Inherits System.Windows.Forms.TextBox
    3.  
    4. #Region " Variables "
    5.  
    6.     Private m_IsCtrl, m_IsAlt, m_IsShift As Boolean
    7.     Private m_Char As Char = CChar(String.Empty)
    8.     Private m_HasChar As Boolean
    9.  
    10. #End Region
    11. #Region " Sub: New "
    12.  
    13.     Public Sub New()
    14.         MyBase.New()
    15.  
    16.         m_IsCtrl = False
    17.         m_IsAlt = False
    18.         m_IsShift = False
    19.         m_HasChar = False
    20.     End Sub
    21.  
    22. #End Region
    23. #Region " Overrides Subs: OnKeyDown, OnKeyUp "
    24.  
    25.     Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
    26.         e.SuppressKeyPress = True
    27.  
    28.         m_IsAlt = e.Alt
    29.         m_IsCtrl = e.Control
    30.         m_IsShift = e.Shift
    31.  
    32.         Select Case e.KeyCode
    33.             Case Keys.ControlKey, Keys.ShiftKey, Keys.Menu
    34.                 'Ignore modifier keys alone.
    35.             Case Else
    36.                 Dim ch As Char = Convert.ToChar(e.KeyCode)
    37.                 If Char.IsLetterOrDigit(ch) Then
    38.                     m_Char = Char.ToUpper(ch)
    39.                     m_HasChar = True
    40.                 End If
    41.         End Select
    42.  
    43.         Call UpdateText()
    44.         MyBase.OnKeyDown(e)
    45.     End Sub
    46.  
    47.     Protected Overrides Sub OnKeyUp(e As System.Windows.Forms.KeyEventArgs)
    48.         If Not m_HasChar Then
    49.             m_IsAlt = e.Alt
    50.             m_IsCtrl = e.Control
    51.             m_IsShift = e.Shift
    52.         End If
    53.  
    54.         Call UpdateText()
    55.  
    56.         If Not e.Alt AndAlso Not e.Control AndAlso Not e.Shift Then
    57.             m_Char = CChar(String.Empty)
    58.             m_HasChar = False
    59.         End If
    60.  
    61.         MyBase.OnKeyDown(e)
    62.     End Sub
    63.  
    64.     Private Sub UpdateText()
    65.         Dim parts As New List(Of String)
    66.  
    67.         If m_IsCtrl Then parts.Add("Ctrl")
    68.         If m_IsShift Then parts.Add("Shift")
    69.         If m_IsAlt Then parts.Add("Alt")
    70.         If m_HasChar Then parts.Add(m_Char.ToString)
    71.  
    72.         Me.Text = String.Join(" + ", parts)
    73.     End Sub
    74.  
    75. #End Region
    76.  
    77. End Class
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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