使用按键模拟短信样式输入 [英] Simulating sms style typing using keypress

查看:85
本文介绍了使用按键模拟短信样式输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能指出我正确的方向,以便能够使用数字键盘上的按键来模拟短信风格的输入吗?

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

我可以获取每个数字以打印出一个字母,但是不确定如何使我的程序将同一键上的多个按键视为相同的事件"(即,如果再次按下该键,则滚动几个字母) (例如2秒)之内.

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

我查找了多个按键,但总是想出组合键(Ctrl,Alt,Delete等).

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

推荐答案

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

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

一些需要跟踪的变量:

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

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

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

这篇关于使用按键模拟短信样式输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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