如何将“密钥”转换为 枚举值转换为“int” C# 中的字符?

发布于 2024-07-17 05:10:39 字数 196 浏览 4 评论 0原文

这看起来应该很容易,但我很难弄清楚这里需要发生什么。

在“KeyDown”事件处理程序中,如果“e.KeyValue”是一个数字,我想将其视为数字并将其存储为 int。 所以,如果我在数字键盘上点击“8”,我不需要“Numpad8”,我想要的是可以加、减或其他的 int 值 8。

那么,如何将 KeyValue 转换为 int 呢?

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. 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.

So, how do I convert from the KeyValue to an int?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

初见 2024-07-24 05:10:39

我会采用这个解决方案:

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);
}

...如果重点是获取所输入的键的标签的数值。

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.

帥小哥 2024-07-24 05:10:39

像这样的东西应该效果很好:(已编辑)

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;
}

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;
}
咿呀咿呀哟 2024-07-24 05:10:39

事实:
键盘有按键。
有些键代表数字,有些则不代表数字。

问题(改写):
如果键代表数字,则生成由键表示的数值。

为了解决这个问题,有必要知道哪些键(所有键的集合中)代表数字以及每个(数字)键代表的确切数值。

据我所知,没有一种简单的方法可以从框架中获取这样的映射。

注意:D0-D9 和 NumPad0-NamPad9 在 Keys 枚举中是连续的这一事实是偶然的,依赖这些值按顺序排序是没有根据的。

所以解决方案是:

  1. 确定给定的键是否代表数字。
  2. 如果 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;
}

可以说,这不是最简单的解决方案,而是紧密模拟该解决方案的解决方案。

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.

So solution would be:

  1. Determine if given key represents a number.
  2. Return numeric value of the key if key represents a number.

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.

等待我真够勒 2024-07-24 05:10:39

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

Depending on how many keys you are keeping track of, the simplest method would be for you to create a select case (or switch statement in C# I guess?) that would check the value of your keydown and depending upon that value assign a value to an integer that is appropriate.

陌若浮生 2024-07-24 05:10:39

如果您想要 NUMERIC 值,则必须创建一个 switch(e.KeyCode) 语句并计算键并创建您自己的 int。 没有简单的方法可以将 Keys 转换为代表键上数字的数值。 您能得到的最接近的可能是 ASCII 等效值,但仍需要翻译。

If you want NUMERIC values, you'll have to create a switch(e.KeyCode) statement and evaluate the key and create your own int. There's no simple way to turn a Keys into a numeric value that represents the number on the key. The closest you could get might be the ASCII equivalent, which would still need to be translated.

Bonjour°[大白 2024-07-24 05:10:39

您可以只监听 KeyPress 事件吗? 它将给出实际按下的字符。

Could you just listen for the KeyPress event instead? It will give the character that was actually pressed.

醉生梦死 2024-07-24 05:10:39

该函数将执行您想要的操作:

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...
    }
}

像这样调用它:

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

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);  
}
路弥 2024-07-24 05:10:39

KeyValue 表示按下的键的字符代码。
为了获得其数值,您应该找到按下的键的字符,然后将其从字符解析为整数。

类似于 Convert.ToInt32(e.KeyCode.ToString())

The KeyValue represents the character code for the key that was pressed.
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())

我不咬妳我踢妳 2024-07-24 05:10:39

或者创建一个扩展方法并像

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

扩展方法所在的地方一样调用它

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...    }

        }
    }
}

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...    }

        }
    }
}
风柔一江水 2024-07-24 05:10:39

我用了这两行简单的代码:

string key = e.KeyCode.ToString();

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

I used this 2 lines simple code:

string key = e.KeyCode.ToString();

if (key.Length > 1) key = key.Replace("NumPad", "").Replace("D", "");
往事风中埋 2024-07-24 05:10:39

这是获取可用值的另一种方法:

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);
}

Carlos Ferreira 发布的替代方法。

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.

爱的十字路口 2024-07-24 05:10:39

int i = (int)e.KeyValue;

int i = (int)e.KeyValue;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文