使用按键模拟短信风格打字

发布于 2024-12-11 20:14:24 字数 180 浏览 0 评论 0原文

谁能给我指明正确的方向,以便能够使用数字键盘上的按键来模拟短信风格的打字?

我可以让每个数字打印出一个字母,但不确定如何让我的程序将同一键上的多次按键视为同一“事件”(即,如果在一段时间内再次按下该键,则滚动浏览多个字母(例如)2 秒)。

我查找了多个按键,但总是想出组合键(ctrl、alt、delete 等)。

Can anyone point me in the right direction to be able to simulate sms style typing using keypress on the number pad?

I can get each number to print out a letter but am unsure of how to get my program to treat a number of keypresses on the same key as the same 'event' (i.e. scrolling through several letters if the key is pressed again within a period of (for example) 2 seconds).

I have looked up multiple keypresses but always come up with key combinations (ctrl, alt, delete etc).

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

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

发布评论

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

评论(2

枉心 2024-12-18 20:14:24

您需要一个状态机并计算每个键的按下次数以确定字母。然后将这些字母(使用事件)传递给应用程序的其余部分。

诗。您是否注意到数字键盘上的数字顺序与电话上的顺序不同? (789 是键盘上的顶行和手机上的底行)

You need a state-machine and count the number of presses on each key to determine the letter. Then pass these letters on (using events) to the rest of your app.

Ps. did you notice that the numbers on a numeric keypad are in a different order than on a phone? (789 are the top row on a keyboard and the bottom row on a phone)

殤城〤 2024-12-18 20:14:24

首先,您需要存储可用的组合:

    static char[] num1 = { 'A', 'B', 'C', '1' };
    static char[] num2 = { 'D', 'E', 'F', '2' };
    // etc...

然后我们制作组合的字典,映射到生成它们的正确键字符:

    Dictionary<char, char[]> map = new Dictionary<char, char[]>()
    {
        {'1', num1},
        {'2', num2} 
    };

一些要跟踪的变量:

    char[] curr = null;
    char currChar = '-';
    int index = 0;

打印函数:

    void Print()
    {
        Console.WriteLine(curr[index]);
    }

逻辑:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (map.ContainsKey(e.KeyChar))
        {
            if (curr == null || e.KeyChar != currChar)
            {
                curr = map[e.KeyChar];
                index = 0;
                currChar = e.KeyChar;
                Print();
            }
            else
            {
                ++index;
                if (index == curr.Length)
                    index = 0;
                Print();
            }
        }
    }

逻辑基本上检查以使得确保我们的键盘映射包含有问题的键码。如果我们没有跟踪任何内容,或者它与我们当前正在跟踪的内容不同,请使用该特定地图和第一个索引。

否则,如果是重复按键,则增加索引(如果经过末尾则循环回到开头)。

Firstly, you need to store the available combinations:

    static char[] num1 = { 'A', 'B', 'C', '1' };
    static char[] num2 = { 'D', 'E', 'F', '2' };
    // etc...

And then we make a dictionary of the combinations, mapped to the right key character that produces them:

    Dictionary<char, char[]> map = new Dictionary<char, char[]>()
    {
        {'1', num1},
        {'2', num2} 
    };

Some variables to keep track:

    char[] curr = null;
    char currChar = '-';
    int index = 0;

A printing function:

    void Print()
    {
        Console.WriteLine(curr[index]);
    }

And the logic:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (map.ContainsKey(e.KeyChar))
        {
            if (curr == null || e.KeyChar != currChar)
            {
                curr = map[e.KeyChar];
                index = 0;
                currChar = e.KeyChar;
                Print();
            }
            else
            {
                ++index;
                if (index == curr.Length)
                    index = 0;
                Print();
            }
        }
    }

The logic basically checks to make sure our keymap contains the keycode in question. If we're not tracking anything, or if it's different to the one we're currently tracking, use that particular map and the first index.

Otherwise, if it's a repeat key-press, increase the index (looping back to the beginning if we pass the end).

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