我正在创建一个winforms应用程序,其目的是使用组合键将IPA符号写入文本框。用户在关注RichTextBox时按下键。某些键组合(例如Ctrl + S)将只生成一个字符并等待下一个按键,例如ʃ
。当您在按住Ctrl的同时反复按相同的键时,其他组合键将产生不同的字符。例如,如果我按下Ctrl然后按A,æ
将被输入,但如果我在按住Ctrl的同时再次按A,则会删除æ
并显示ɑ
。它将继续循环通过这两个键,直到播放器释放Ctrl或按下不同的键。
Private Sub IPAtext_KeyPress(sender As Object, e As KeyEventArgs) Handles IPAtext.KeyDown
Dim AChars() As Char = {"æ", "ɑ"}
Dim Counter As Integer = 0
Select Case e.Modifiers
Case Keys.Control
Select Case e.KeyCode
Case Keys.A
While e.Modifiers = Keys.Control And e.KeyCode = Keys.A
IPAtext.AppendText(AChars(Counter))
'-- Pause for keypress. If keypress =/= A then complete loop, if keypress is A again then increase counter. --
Counter = (Counter + 1) Mod AChars.Count 'Increments the counter so that it cycles back to 0 when it goes past the count of AChars.
'-- Only execute the line below if keypress is A. --
IPAtext.Text = IPAtext.Text.Remove(IPAtext.TextLength - 1) 'Remove the last character from the textbox.
End While
End Select
End Select
End Sub
我还应该注意到,我试图在某种程度上模拟this IPA web app的行为。
答案 0 :(得分:0)
这是我在尝试时想到的。在KeyDown事件上设置处理程序,然后默认压缩所有KeyPresses。然后看看你是否可以将当前的keyCode与你期望特殊的Case相匹配。否则,取消注册KeyEventArgs对象并保留Sub。
我的代码将选择您希望循环的文本,因此您可以手动选择一个字符和"继续"即使Ctrl被释放也会循环播放。你必须根据自己的喜好修改它,但这对你来说至少是一个开始。
Private Sub RichTextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
e.SuppressKeyPress = True
e.Handled = True
Select Case e.KeyCode
Case Keys.A And e.Modifiers = Keys.Control
RichTextBox1.SelectedText = GetCharacterToPrint(RichTextBox1.SelectedText, New Char() {"æ", "ɑ"})
RichTextBox1.Select(RichTextBox1.SelectionStart - 1, 1)
Case Else
e.Handled = False
e.SuppressKeyPress = False
End Select
End Sub
这是一个返回下一个字符的简短函数:
Private Function GetCharacterToPrint(ByVal SelectedText As String, ByVal Characters As Char()) As Char
If SelectedText.Length <> 1 Then
Return Characters(0)
End If
Dim c As Char = SelectedText(0)
Dim index As Integer = String.Join("", Characters).IndexOf(c)
If index = Characters.Length - 1 Then
Return Characters(0)
Else
Return Characters(index + 1)
End If
End Function
最后如何在ControlKey发布后继续:
Private Sub RichTextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
If e.KeyValue = Keys.ControlKey Then
RichTextBox1.SelectionStart += 1
End If
End Sub