2017-03-24 56 views
1

當我嘗試將ASCII整數轉換爲具有多個字符的鍵時,我總是得到不正確的結果。 A - > z/0 - > 9正常工作,但任何具有多個字符的按鍵總是給出不正確的結果,例如F1-> F12/Numlock 0-> Numlock 9/Home鍵等。如何正確地將ascii整數轉換爲相應的按鍵?

示例爲F3 ASCII碼爲114,但是當我轉換114字符它總是輸出的R代替F3

這類似於什麼我都試過的東西:

(它在這個例子似乎無用,但該ISN我完全不知道如何使用它)

Dim bind = "114" '(F3) depending on user input 
Dim c As Char = bind 'Convert the ASCII integer(bind) to character(c) 
TextBox1.text = c 'which in this case outputs as "r" 

如何解決此問題以便114/any other key that has more than one character將輸出爲F3/their correct value

編輯,更新例如:

該代碼可用於用戶定義的快捷鍵,其需要的應用程序之外的工作,所以我用GetAsyncKeyState以確定哪個ASCII整數被按下,然後使用針對該ASCII整數綁定,但我還需要將該整數轉換爲字符(本例中爲F3),以便用戶知道哪個keybind當前處於活動狀態。

Do 
     ExitLoop = False 

     For i = 0 To 255 
      result = 0 
      result = GetAsyncKeyState(i) 
      If result = -32767 Then 

       Dim c As Char = Chr(i)  ' Convert ASCII integer to char. 
       Bind1.Text = c 
       Shortcut1 = i 
       ExitLoop = True 

      End If 
     Next i 

    Loop While ExitLoop = False 
+2

_114_是'r' [I相信](http://www.theasciicode.com.ar/ascii-printable-characters/lowercase-letter-r-minuscule-ascii-code-114.html)以及是F3。 – Bugs

+1

是的,114是小寫字母r的ASCII碼。查看此圖片以查看關鍵代碼的完整視圖https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/ASCII-Table-wide.svg/1280px-ASCII-Table-wide.svg.png – David

+0

你是說他們可以輸入114,你想知道角色是什麼?在F3鍵上使用相同的邏輯?他們輸入一個等於F3鍵的數字? – Bugs

回答

1

標題有點誤導,因爲它不是解決問題的正確方法。您有GetAsyncKeyState,它告訴您有關索引按照其Virtual-Key Code傳遞的密鑰的狀態。返回的值告訴我們鍵是否被按下。從MSDN頁面,

如果設置了最高有效位,則該鍵關閉,如果設置了最低有效位,則在先前調用GetAsyncKeyState之後按下該鍵。

由於返回值是一個UInt16,我們想看看最顯著位爲1,我們將And與0xFFFF的結果(VB,& HFFFF)。

下面的程序將報告全部當前被按下的鍵,並將它們放在一個消息中,並將該消息放在窗體的標題欄中。製作一個新的WinForms項目,並粘貼此代碼。您可能需要從設計師背後的代碼中刪除Dispose。功能GetVKeyPressed支持的密鑰數量有限,但您可以根據虛擬密鑰代碼頁面製作所需的案例。

Public Class Form1 
    Implements IDisposable 

    Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Integer) As UInt16 

    Private timer As New System.Threading.Timer(AddressOf timerCallback, Nothing, -1, -1) 

    Private Function GetVKeyPressed(vKey As Integer) As String 
     ' see https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx 
     Select Case vKey 
      Case &H30 To &H39 ' 0 to 9 
       Return Chr(vKey).ToString() 
      Case &H41 To &H5A ' A to Z 
       Return Chr(vKey).ToString() 
      Case &H70 To &H87 
       Return "F" & (vKey - &H70 + 1) 
      Case &H1 
       Return "LMB" 
      Case &H2 
       Return "RMB" 
      Case Else 
       Return "Key not supported yet!" 
     End Select 
    End Function 

    Private Sub timerCallback(state As Object) 
     Dim results As New Dictionary(Of Integer, UInt16)() 
     For i = 0 To 255 
      results.Add(i, GetAsyncKeyState(i)) 
     Next i 
     Dim pressedKeys = String.Join(", ", results. 
      Where(Function(kvp) kvp.Value And &HFFFF). 
      Select(Function(kvp) GetVKeyPressed(kvp.Key))) 
     Try 
      Me.Invoke(Sub() Me.Text = pressedKeys) 
     Catch ex As ObjectDisposedException 
     End Try 
     timer.Change(100, -1) 
    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
     timer.Change(100, -1) 
    End Sub 

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed 
     timer.Change(-1, -1) 
    End Sub 

    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
     Try 
      If disposing AndAlso components IsNot Nothing Then 
       components.Dispose() 
       timer.Dispose() 
      End If 
     Finally 
      MyBase.Dispose(disposing) 
     End Try 
    End Sub 

End Class 
+0

我知道我已經晚了幾個月,但我只想指出,所有'VK_ *'虛擬鍵碼都被映射到[**'System.Windows.Forms.Keys'枚舉**](https ://msdn.microsoft.com/en-us/library/system.windows.forms.keys(v = vs.110).aspx),所以爲了簡單起見,它可以用作'GetAsyncKeyState()'的參數類型。 :) –

相關問題