简体   繁体   English

如何在 C# 中将“Keys”枚举值转换为“int”字符?

[英]How do I convert a "Keys" enum value to an "int" character in C#?

This seems like something that should be easy, but I am having a tough time figuring out what needs to happen here.这似乎应该很容易,但我很难弄清楚这里需要发生什么。

In the "KeyDown" eventhandler, if the "e.KeyValue" is a number, I want to treat it as a number and store it as an int.在“KeyDown”事件处理程序中,如果“e.KeyValue”是一个数字,我想将其视为一个数字并将其存储为一个 int。 So, if I hit "8" on the number pad, I don't want "Numpad8" I want the int value 8 that I can add or subtract or whatever.所以,如果我在数字键盘上点击“8”,我不想要“Numpad8”,我想要可以添加或减去的 int 值 8 等等。

So, how do I convert from the KeyValue to an int?那么,如何将 KeyValue 转换为 int?

I'd go with this solution: 我会采用以下解决方案:

int value = -1;
if (e.KeyValue >= ((int) Keys.NumPad0) && e.KeyValue <= ((int) Keys.NumPad9)) { // numpad
    value = e.KeyValue - ((int) Keys.NumPad0);
} else if (e.KeyValue >= ((int) Keys.D0) && e.KeyValue <= ((int) Keys.D9)) { // regular numbers
    value = e.KeyValue - ((int) Keys.D0);
}

...if the point was to get the numeric value of the label of they key that was punched in. ...如果重点是要获取打入的键的标签的数值。

Something like this should work well: (Edited) 这样的事情应该可以正常工作:(已编辑)

int keyVal = (int)e.KeyValue;
int value = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9)
{
    value = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
    value = (int)e.KeyValue - (int)Keys.NumPad0;
}

Facts: Keyboard has keys. 事实:键盘有按键。 Some keys represent numbers and some do not. 有些键代表数字,有些则不能。

Problem (rephrased): Yield a numeric value represented by a key, if the key represents a number. 问题(改写):如果键代表数字,则产生由键代表的数字值。

To solve the problem it is necessary to know which keys (out of set of all keys) represent numbers as well as exact numeric value each (number) key represents. 为了解决该问题,必须知道哪些键(在所有键集中)代表数字以及每个(数字)键代表的确切数值。

To my knowledge there is no an easy way to get such a mapping from the framework. 据我所知,没有一种简单的方法可以从框架中获得这种映射。

Note: The fact D0-D9 and NumPad0-NamPad9 are sequential in the Keys enum is accidental and relying on these values being ordered sequentially is unfounded. 注意:D0-D9和NumPad0-NamPad9在Keys枚举中是顺序的事实是偶然的,并且没有理由依赖于这些值的顺序排序。

So solution would be: 所以解决方案是:

  1. Determine if given key represents a number. 确定给定的键是否代表数字。
  2. Return numeric value of the key if key represents a number. 如果key代表数字,则返回key的数值。

private static readonly IDictionary<Keys, int> NumericKeys = 
    new Dictionary<Keys, int> {
        { Keys.D0, 0 },
        { Keys.D1, 1 },
        { Keys.D2, 2 },
        { Keys.D3, 3 },
        { Keys.D4, 4 },
        { Keys.D5, 5 },
        { Keys.D6, 6 },
        { Keys.D7, 7 },
        { Keys.D8, 8 },
        { Keys.D9, 9 },
        { Keys.NumPad0, 0 },
        { Keys.NumPad1, 1 },
        { Keys.NumPad2, 2 },
        { Keys.NumPad3, 3 },
        { Keys.NumPad4, 4 },
        { Keys.NumPad5, 5 },
        { Keys.NumPad6, 6 },
        { Keys.NumPad7, 7 },
        { Keys.NumPad8, 8 },
        { Keys.NumPad9, 9 }
   };

private int? GetKeyNumericValue(KeyEventArgs e) {
    if (NumericKeys.ContainsKey(e.KeyCode)) return NumericKeys[e.KeyCode];
    else return null;
}

Arguably, not the simplest solution, but the one that models the solution closely. 可以说,这不是最简单的解决方案,而是对解决方案进行建模的一种方法。

根据要跟踪的键数,最简单的方法是为您创建一个选择案例(我猜是C#中的switch语句?),该案例将检查keydown的值,并根据该值将值分配给一个合适的整数。

If you want NUMERIC values, you'll have to create a switch(e.KeyCode) statement and evaluate the key and create your own int. 如果需要NUMERIC值,则必须创建一个switch(e.KeyCode)语句并评估键并创建自己的int。 There's no simple way to turn a Keys into a numeric value that represents the number on the key. 没有简单的方法可以将“ Keys转换为代表Keys数字的数字值。 The closest you could get might be the ASCII equivalent, which would still need to be translated. 您可以获得的最接近的值可能是ASCII等效值,仍然需要翻译。

Could you just listen for the KeyPress event instead? 您可以只听KeyPress事件吗? It will give the character that was actually pressed. 它将给出实际按下的字符。

This function will do what you want: 此函数将执行您想要的操作:

private int GetKeyValue(int keyValue)
{
    if (keyValue >= 48 && keyValue <= 57)
    {
        return keyValue - 48;
    }
    else if (keyValue >= 96 && keyValue <= 105)
    {
        return keyValue - 96;
    }
    else
    {
        return -1; // Not a number... do whatever...
    }
}

Call it like so: 这样称呼它:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    int num = GetKeyValue(e.KeyValue);  
}

I used this 2 lines simple code: 我使用了以下两行简单代码:

string key = e.KeyCode.ToString();

if (key.Length > 1) key = key.Replace("NumPad", "").Replace("D", "");

here is another way on how to get a usable value:这是获取可用值的另一种方法:

string key
if (((int)e.Key < 84 && (int)e.Key > 73) || ((int)e.Key < 44 && (int)e.Key > 33))
{
    key = ((int)e.Key - 4).ToString();
    key = key.Remove(0,1);
}

Alternative to what Carlos Ferreira posted.替代 Carlos Ferreira 发布的内容。

The KeyValue represents the character code for the key that was pressed. KeyValue表示所按下键的字符代码。 In order to obtain its numerical valaue you should find the character for the key that was pressed, then parse it from a character to an integer. 为了获得其数字值,您应该找到所按下键的字符,然后将其从字符解析为整数。

Something like Convert.ToInt32(e.KeyCode.ToString()) 像Convert.ToInt32(e.KeyCode.ToString())之类的东西

Or create a extension method and call it like 或创建一个扩展方法并像调用它一样

private void Form1_KeyDown(object sender, KeyEventArgs e)
{    
    int num = e.KeyValue.GetKeyValue();  
}

where the extension method is 扩展方法在哪里

public static class MyExtensions
{
    public static int GetKeyValue(this int keyValue)
    {

        if (keyValue >= 48 && keyValue <= 57)
        {
            return keyValue - 48;
        }
        else if (keyValue >= 96 && keyValue <= 105)
        {
            return keyValue - 96;
        }
        else
        {
            return -1; // Not a number... do whatever...    }

        }
    }
}

int i =(int)e.KeyValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM