地图编号到相应的字母

时间:2014-12-13 14:57:16

标签: wpf vb.net

我正在编写一个专门用于遥控器的应用程序。

我希望能够使用遥控器上的数字键盘(使用按钮上印刷的相应字母)转到ListView的某个字母,例如:

  • 2 = A,B或C
  • 3 = D,E或F

目前我已经使用了第一个字母,因此按下了2,它将ListView滚动到A#。

我想更进一步,让它像旧手机短信一样工作。因此,如果按下一次2,它将转到A,如果再次按下2(在大约2秒内),它将转到B,依此类推。

我不确定如何做到这一点,也许我的能力超出了我的能力范围。我将不胜感激任何提示或者您过去可能使用的一些示例代码来执行类似的操作。

这是我目前的代码,仅限第一个字母:

Sub doLookup(ByVal input As String)
    For i As Integer = 0 To Showslist.Items.Count - 1
        Dim selected As Object = Showslist.Items.Item(i)
        Dim name As String = selected.name
        name = name.ToLower
        If name.StartsWith(input.ToLower) Then
            Showslist.SelectedIndex = i
            Showslist.ScrollIntoView(Showslist.SelectedItem)
            Exit For
        End If
    Next
End Sub

Private Sub Showslist_KeyUp(sender As Object, e As KeyEventArgs) Handles Showslist.KeyUp

    Case Key.Return
        Case Key.D2
            doLookup("a")
            Exit Select
        Case Key.D3
            doLookup("d")
            Exit Select
        Case Key.D4
            doLookup("g")
            Exit Select
        Case Key.D5
            doLookup("j")
            Exit Select
        Case Key.D6
            doLookup("m")
            Exit Select
        Case Key.D7
            doLookup("p")
            Exit Select
        Case Key.D8
            doLookup("t")
            Exit Select
        Case Key.D9
            doLookup("w")
            Exit Select
        Case Key.D1
            doLookup("1")
            Exit Select
        Case Key.D0
            Showslist.SelectedIndex = 0
            Showslist.ScrollIntoView(Showslist.SelectedItem)
            Exit Select

    End Select
End Sub

2 个答案:

答案 0 :(得分:3)

考虑问题的一种方法是了解按键的当前状态。从#2到#8的每个数字键都有三种状态(key.D2,“a”,“b”,“c”)(key.D3,“d”,“e”,“f”),除了# 9键有四种状态(key.D9,“w”,“x”,“y”,“z”)*

**假设使用美国英语键盘。

我的解决方案是存储在私有变量中按下的键。我还存储了按下该键的次数。

Private currentKey As Key
Private currentKeyPresses As Integer

使用Mod运算符将数字保持在正确的范围

currentKeyPresses = (currentKeyPresses + 1) Mod 3

现在按#2键循环显示值0,1,2,0,1,2等。 按下新的数字键后,请重置currentKeycurrentKeyPresses变量。

currentKey = e.Key
currentKeyPresses = 0

我将状态存储代码放在KeyDown事件中,并修改了KeyUp事件代码,以便为每个按键查找正确的字母。

<强>代码

  Private currentKey As Key
  Private currentKeyPresses As Integer
  Private currentLetter As String
  Private letters As String = "abcdefghijlmnopqrstuvwxyz"

  Private Sub Showslist_KeyDown(sender As Object, e As KeyEventArgs) Handles Showslist.KeyDown
    If currentKey = e.Key Then
      If currentKey = Key.D9 Then
        ' the D9 key contains (4) letters (WXYZ)
        currentKeyPresses = (currentKeyPresses + 1) Mod 4

      Else
        ' assumes there are only three letters per key
        ' e.g D2 contains (3) letters (ABC)
        currentKeyPresses = (currentKeyPresses + 1) Mod 3
      End If

    Else
      ' this is the first time using that digit key
      currentKey = e.Key
      currentKeyPresses = 0

    End If

  End Sub

  Private Sub Showslist_KeyUp(sender As Object, e As KeyEventArgs) Handles Showslist.KeyUp
    Dim currentLetter As String = ""

    Select Case e.Key
      Case Key.Return
      Case Key.D2
        currentLetter = GetLetterForDKey(0, currentKeyPresses)
      Case Key.D3
        currentLetter = GetLetterForDKey(3, currentKeyPresses)
      Case Key.D4
        currentLetter = GetLetterForDKey(6, currentKeyPresses)
      Case Key.D5
        currentLetter = GetLetterForDKey(9, currentKeyPresses)
      Case Key.D6
        currentLetter = GetLetterForDKey(12, currentKeyPresses)
      Case Key.D7
        currentLetter = GetLetterForDKey(15, currentKeyPresses)
      Case Key.D8
        currentLetter = GetLetterForDKey(18, currentKeyPresses)
      Case Key.D9
        currentLetter = GetLetterForDKey(21, currentKeyPresses)
      Case Key.D1
        doLookup("1")
      Case Key.D0
        Showslist.SelectedIndex = 0
        Showslist.ScrollIntoView(Showslist.SelectedItem)
        Exit Select

    End Select
    doLookup(currentLetter)
  End Sub

  Private Function GetLetterForDKey(startPosition As Integer, pressCount As Integer) As String

    Return letters.Substring(startPosition + pressCount, 1)

  End Function

答案 1 :(得分:0)

只需添加一个秒表

private StopWatch sw = new StopWatch();

Private Sub Showslist_KeyUp 
{
    sw.Stop();
    bool secondary = (sw.ElasedMillseconds > 0 and sw.ElasedMillseconds < 200);
    sw.Restart();
    ....