.net 将键码转换为键字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:26:41  来源:igfitidea点击:

Convert keycode to key char

.net

提问by

How do I convert a keycode to a keychar in .NET?

如何在 .NET 中将键码转换为键字符?

回答by Ady

In VB.NET:

在 VB.NET 中:

ChrW(70)

In C# you can cast:

在 C# 中,您可以转换:

(char) 70

回答by Pablo Rausch

I solved this using a library class from user32.dll. I would prefer a Framework class, but couldn't find one so this worked for me.

我使用 user32.dll 中的库类解决了这个问题。我更喜欢一个框架类,但找不到一个所以这对我有用。

Imports System.Runtime.InteropServices

Public Class KeyCodeToAscii

    <DllImport("User32.dll")> _
    Public Shared Function ToAscii(ByVal uVirtKey As Integer, _
                                   ByVal uScanCode As Integer, _
                                   ByVal lpbKeyState As Byte(), _
                                   ByVal lpChar As Byte(), _
                                   ByVal uFlags As Integer) _
                                   As Integer
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function GetKeyboardState(ByVal pbKeyState As Byte()) _
      As Integer

    End Function

    Public Shared Function GetAsciiCharacter(ByVal uVirtKey As Integer) _
      As Char

        Dim lpKeyState As Byte() = New Byte(255) {}
        GetKeyboardState(lpKeyState)
        Dim lpChar As Byte() = New Byte(1) {}
        If ToAscii(uVirtKey, 0, lpKeyState, lpChar, 0) = 1 Then
            Return Convert.ToChar((lpChar(0)))
        Else
            Return New Char()
        End If
    End Function

End Class

回答by Hasitha D

Try this:

尝试这个:

Char.ConvertFromUtf32(e.KeyValue)

回答by MRb

I found this snippet to be rather helpful in detecting a keypress from the enter key:

我发现这个片段对于从 Enter 键检测按键非常有帮助:

Private Sub StartValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles StartValue.KeyPress
    If ChrW(13).ToString = e.KeyChar And StartValue.Text = "" Then
        StartValue.Text = "0"
    End If
End Sub

I found this to be a decent solution to having people trying to submit a null value in a textbox. This just forces a 0, but it uses the "detect enter being pressed" to run.

我发现这是让人们尝试在文本框中提交空值的一个不错的解决方案。这只是强制为 0,但它使用“检测输入被按下”来运行。

回答by DaveH

Convert.ToChar(Keys.Enter);

Convert.ToChar(Keys.Enter);

回答by Yiu Korochko

I can't seem to reply to DaveH, but if you do as he says, it works fine!

我似乎无法回复 DaveH,但如果你按照他说的去做,那就没问题了!

  Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
  Public sub harry()
    For i = 1 to 255
      If Chr(i) = Convert.ToChar(Keys.Enter) Then
          Label1.Text &= " [Enter] "
      ElseIf Chr(i) = Convert.ToChar(Keys.Delete) Then
          Label1.Text &= " [Delete] "
      End If
    Next i
  End Sub

回答by Nooa

Here an example from my side :)

这是我身边的一个例子:)

#region Just for fun :D

private string _lastKeys = "";

private readonly Dictionary<string, string[]> _keyChecker = _
    new Dictionary<string, string[]>
        {
            // Key code to press, msgbox header, msgbox text
            {"iddqd", new[] {"Cheater! :-)", "Godmode activated!"}},
            {"idkfa", new[] {"Cheater! :-)", "All Weapons unlocked!"}},
            {"aAa", new[] {"Testing", "Test!"}},
            {"aaa", new[] {"function", "close"}}
        };

private void KeyChecker(KeyPressEventArgs e)
{
    _lastKeys += e.KeyChar;

    foreach (var key in _keyChecker.Keys)
        if (_lastKeys.EndsWith(key))
            if (_keyChecker[key][0] != "function")
                MessageBox.Show(_keyChecker[key][1], _keyChecker[key][0]);
            else
                KeyCheckerFunction(_keyChecker[key][1]);

    while (_lastKeys.Length > 100)
        _lastKeys = _lastKeys.Substring(1);
}

private void KeyCheckerFunction(string func)
{
    switch (func)
    {
        case "close":
            Close();
            break;
    }
}

private void FormMain_KeyPress(object sender, KeyPressEventArgs e)
{
    KeyChecker(e);
}

#endregion Just for fun :D

回答by Nooa

String.fromCharCode(event.keyCode)

String.fromCharCode(event.keyCode)