【问题标题】:How to detect when (forward) slash key is pressed in OEM keys C#如何检测在 OEM 键 C# 中何时按下(正向)斜杠键
【发布时间】:2017-06-22 20:10:17
【问题描述】:

我正在开发 wpf c# 应用程序,我需要检测用户何时按下“/”,但无法找到“/”e.Key ,我看到有Key.OemBackslash 之类的东西,但我找不到“/”(正斜杠)的正确事件...

谢谢各位, 干杯

【问题讨论】:

  • e.KeyChar 怎么样?
  • @SelmanGenç wpf 中没有 e.KeyChar,我应该包含 windows 表单库,否则它会弄乱我以 win 形式存在的 MessageBoxes

标签: c# wpf key slash


【解决方案1】:

在美式键盘上应该是Key.OemQuestion。但是在瑞典语键盘上它是D7,所以这取决于。键盘上的键并不总是产生相同的字符。

根据您要执行的操作,您最好处理 PreviewTextInput 事件:

protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
    base.OnPreviewTextInput(e);
    if (e.Text == "/")
    {
        Debug.WriteLine("...");
    }
}

【讨论】:

  • @我正在使用 WPF,我正在使用此事件 Window_PreviewKeyDown 并且看起来没有适合 e.. 的文本。
  • 不,没有。这就是为什么您可以处理 WPF 窗口的 PreviewTextInput 而不是 PreviewKeyDown 事件...否则 Key.OemQuestion 是您最接近“/”键的原因。
  • 顺便说一句,有没有办法检测何时按下回车?例如,首先检查 e.Text,然后检查是否按下了 enter,如果我的发件人不是 KeyEventArgs,而不是 TextCompositionEventArgs .. 并且 TextCompositionEventArgs 不包含 e.Key== Key,我该怎么做。返回..
  • 处理 PreviewKeyDown 事件以检测 ENTER 按键和 PreviewTextInput 事件以检测“/”? ENTER 不是字符,“/”不是键。
【解决方案2】:

您可以通过以下方法(see this site)从键中获取字符。

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
        bool toUnicodeIsTrue=false;
        char t = GetCharFromKey(e.Key, ref toUnicodeIsTrue);
        if ( t == '/')
        {
            // do stuff
        }
        base.OnPreviewKeyDown(e);  
}


    public static char GetCharFromKey(System.Windows.Input.Key key, ref bool toUnicodeIsTrue)
    {
        toUnicodeIsTrue = true;
        char ch = ' ';

        // First, you need to get the VirtualKey code. Thankfully, there’s a simple class 
        // called KeyInterop, which exposes a static method VirtualKeyFromKey 
        // that gets us this information
        int virtualKey = System.Windows.Input.KeyInterop.VirtualKeyFromKey(key);
        //Then, we need to get the character. This is much trickier. 
        //First we have to get the keyboard state and then we have to map that VirtualKey 
        //we got in the first step to a ScanCode, and finally, convert all of that to Unicode, 
        //because .Net doesn’t really speak ASCII
        byte[] keyboardState = new byte[256];
        GetKeyboardState(keyboardState);

        uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
        StringBuilder stringBuilder = new StringBuilder(2);

        int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
        switch (result)
        {
            case -1:
                toUnicodeIsTrue = false;
                break;
            case 0:
                toUnicodeIsTrue = false;
                break;
            case 1:
                {
                    ch = stringBuilder[0];
                    break;
                }
            default:
                {
                    ch = stringBuilder[0];
                    break;
                }
        }
        return ch;
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool GetKeyboardState(byte[] lpKeyState);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
    public enum MapType : uint
    {
        MAPVK_VK_TO_VSC = 0x0,
        MAPVK_VSC_TO_VK = 0x1,
        MAPVK_VK_TO_CHAR = 0x2,
        MAPVK_VSC_TO_VK_EX = 0x3,
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int ToUnicode(
        uint wVirtKey,
        uint wScanCode,
        byte[] lpKeyState,
        [System.Runtime.InteropServices.Out, System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr, SizeParamIndex = 4)]
        StringBuilder pwszBuff,
        int cchBuff,
        uint wFlags);
}

我的旧答案:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{ //***
    if (e.Key == Key.Oem2)
    {  
        // do stuff
    }
    base.OnPreviewKeyDown(e);
}

请注意,名称以“oem”(原始设备制造商)开头,这意味着键盘制造商对其功能负责,并且在本地键盘中有所不同。所以,你可以在

中设置一个断点
{//*** 

我的代码行并检查 e.Key 属性。

【讨论】:

  • 不幸的是,当我点击正斜杠时 Key.Oem2 没有被触发:(
猜你喜欢
  • 1970-01-01
  • 2010-10-24
  • 2011-02-26
  • 1970-01-01
  • 2018-02-15
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多