将字符转换为密钥 [英] Convert A Char To Keys

查看:130
本文介绍了将字符转换为密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊的char(/@),我想将其转换为Keys.

I have a special char (/ @) That I want to convert to Keys.

我当前正在使用它:

Keys k = (Keys)'/';

在调试时,我得到k等于:

And while debugging, I get that k equals to :

LButton | RButton | MButton |返回|空间类型-System.Windows.Forms.Keys

LButton | RButton | MButton | Back | Space type - System.Windows.Forms.Keys

k的键码假定为111.

k's keycode was suppose to be 111.

注意:该代码确实适用于大写字母,例如:

NOTE: The code does work for uppercase letters such as :

Keys k = (Keys)'Z';

在这种情况下,k的密钥代码为90,可以.

In that case, k's key code is 90, which is ok.

我正在尝试找到一种将特殊字符转换为Keys的方法. (或正确的密钥代码)

I'm trying to find a way to convert special chars to Keys. (or to their proper key code)

尝试使用:全局发送密钥

Trying to send keys globally using :

public static void SendKey(byte keycode)
    {
        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP = 0x2;
        keybd_event(keycode, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
        keybd_event(keycode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
    }

SendKey((byte)Keys.{SomethingHere});

推荐答案

您不能从char强制转换为Keys.键是一个标志枚举,表示在键盘上按下了哪些物理键.您正在尝试的问题可能不是要解决的直接问题.谁说用户使用什么键盘以及什么映射在用户的键盘及其区域设置上生成特定字符?

You can't cast from char to Keys. Keys is a flags enumeration that represents which physical keys are pressed on the keyboard. What you are trying might not be a straightforward problem to solve. Who's to say what keyboard the user used and what mapping generates what particular character on the user's keyboard and their locale?

您能更具体地说明您的目标吗?您是否要教用户如何键入特殊字符或其他内容?

Can you be more specific about your goal? Are you trying to teach a user how to type a special character or something else?

根据您的更新,您应该使用 SendKeys 类.

Based on your update, you should be using the SendKeys class.

这篇关于将字符转换为密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆