Results 1 to 3 of 3

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

Threaded View

  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

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