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:
Public Class HotKeyTextBox
Inherits System.Windows.Forms.TextBox
#Region " Variables "
Private m_IsCtrl, m_IsAlt, m_IsShift As Boolean
Private m_Char As Char = CChar(String.Empty)
Private m_HasChar As Boolean
#End Region
#Region " Sub: New "
Public Sub New()
MyBase.New()
m_IsCtrl = False
m_IsAlt = False
m_IsShift = False
m_HasChar = False
End Sub
#End Region
#Region " Overrides Subs: OnKeyDown, OnKeyUp "
Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
e.SuppressKeyPress = True
m_IsAlt = e.Alt
m_IsCtrl = e.Control
m_IsShift = e.Shift
Select Case e.KeyCode
Case Keys.ControlKey, Keys.ShiftKey, Keys.Menu
'Ignore modifier keys alone.
Case Else
Dim ch As Char = Convert.ToChar(e.KeyCode)
If Char.IsLetterOrDigit(ch) Then
m_Char = Char.ToUpper(ch)
m_HasChar = True
End If
End Select
Call UpdateText()
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnKeyUp(e As System.Windows.Forms.KeyEventArgs)
If Not m_HasChar Then
m_IsAlt = e.Alt
m_IsCtrl = e.Control
m_IsShift = e.Shift
End If
Call UpdateText()
If Not e.Alt AndAlso Not e.Control AndAlso Not e.Shift Then
m_Char = CChar(String.Empty)
m_HasChar = False
End If
MyBase.OnKeyDown(e)
End Sub
Private Sub UpdateText()
Dim parts As New List(Of String)
If m_IsCtrl Then parts.Add("Ctrl")
If m_IsShift Then parts.Add("Shift")
If m_IsAlt Then parts.Add("Alt")
If m_HasChar Then parts.Add(m_Char.ToString)
Me.Text = String.Join(" + ", parts)
End Sub
#End Region
End Class