键盘功能C# [英] Keyboard Functions C#

查看:51
本文介绍了键盘功能C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个触摸屏键盘,我已经创建了所有字母和东西,但是问题是我有多个文本框希望输入.

以下是一段代码:

Hi, I am making a touch screen keyboard i have created all the letters and stuff however the issue is I have multiple text boxes I wish to be able to type in.

Below is a segment of the code:

private void btnS_Click(object sender, EventArgs e)
{
    if (capsLock == 0)
    {
        textBox1.Text += ("S");
    }
    else if (capsLock == 1)
    {
        textBox1.Text += ("s");
    }
}



基本上,它仅针对textBox1,用什么命令可以在Target文本框中输入字母? (光标所在的位置).
谢谢

编辑
任何人将按钮设置为Focusable = false都有帮助吗?


[Edit]取消选中忽略文本中的HTML" [/Edit]



Basically its only targeted at textBox1 what command would make it enter the letter in the Target textbox? (where the cursor is).
Thanks

Edit
Anyone got any help for setting a button to Focusable = false?


unchecked "Ignore HTML in text"[/Edit]

推荐答案

您可以使用TextBox ="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focused.aspx"> Control.Focused [ [
You can get the focused TextBox using Control.Focused[^] property. See this[^] example.

[UPDATE]
My guess is that you want to append text to the TextBox that is focused. You could write your own helper method say void AppendText(string strToAppend) that checks for the active TextBox and append the given string to it.

For example if you have 2 TextBoxes (textBox1 and textBox2) than you could write get focused TexBox method like this:
private TextBox GetFocusedTextBox()
{
    if (textBox1.Focused == true)
    {
        return textBox1;
    }
    if (textBox2.Focused == true)
    {
        return textBox2;
    }

    // Here you can add more...

    // There is no focused textbox
    return null;
}



如果您有很多文本框,则可以使用我给您的链接中的第一个示例,更改GetFocusedTextBox()方法以遍历表单上的控件.
您的附加文本方法可能类似于:



If you have lots of textboxes then you can change the GetFocusedTextBox() method to iterate through the controls on the form using the first example from the link that I gave you.
Your append text method could be something like:

private void AppendText(string strToAppend)
{
    TextBox tbFocused = GetFocusedTextBox();
    if(tbFocused != null)
    {
       tbFocused.Text += strToAppend;
    }
    else
    {
       // There is no focused textbox, you should decide what to do
    }
}



这是一个简单的解决方案,您可以调用AppendText方法,在该方法中要将文本追加到焦点TextBox处.我希望这有帮助.如果这还不够或不清楚,请随时发表评论. :)
[/UPDATE]

[UPDATE 2.1]
对不起,我迟迟未答复,我的互联网连接断开了,但我在这里为您提供了一个可行的示例. :)
首先,我将粘贴代码,然后尝试解释:



This is an easy solution and you can call the AppendText method where you want to append text to the focused TextBox. I hope this helps. Feel free to post a comment in case this not enough or not clear. :)
[/UPDATE]

[UPDATE 2.1]
Sorry for late answer, my internet connection was down but I''m here with a working example for you. :)
First I will paste the code and after that will try to explain:

public partial class Form1 : Form
{
    // These are members that we will use to restore
    // the selection of last selected textbox,
    // restore the caret position and selection length
    private TextBox tbSelected; // Last focused TextBox
    private int posCaret;       // Caret position
    private int selLength;      // Selection length
    public Form1()
    {
        InitializeComponent();
        // We will use leave event for textboxes
        textBox1.Leave += new System.EventHandler(textBox_Leave);
        textBox2.Leave += new System.EventHandler(textBox_Leave);
        // Set initial selection to the first textbox
        textBox1.Select();
        tbSelected = textBox1;
        posCaret = 0;
        selLength = 0;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        // Call this to restore the last selection
        RestoreLastSelection();
        // Set the selected text
        tbSelected.SelectedText = "S";
    }
    private void button2_Click(object sender, EventArgs e)
    {
        // Call this to restore the last selection
        RestoreLastSelection();
        // Set the selected text
        tbSelected.SelectedText = "FOO";
    }
    // Leave event handler
    private void textBox_Leave(object sender, EventArgs e)
    {
        // Remember the last focused thextbox,
        // the caret position in it and the selection length
        tbSelected = (TextBox)sender;
        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;
    }
    // Helper method to restore selection
    private void RestoreLastSelection()
    {
        tbSelected.Select();
        posCaret = tbSelected.SelectionStart;
        selLength = tbSelected.SelectionLength;
    }
}


我创建了一个新项目(Windows窗体应用程序),其中添加了2个按钮和2个文本框.我的方法很简单.看看
Control.Leave [ ^ ]事件.我使用此事件来注册最后一个重点突出的文本框,插入符号的位置以及所选文本的长度.单击按钮后,我将恢复文本框中的选择,然后替换所选的文本.这样,您可以将文本添加到光标位置和/或替换选定的文本.检查上面的来源.我相信这种方法很容易理解和使用.试试看,让我知道是否有问题. :)
[/UPDATE 2.1]


I''ve created a new project (Windows forms application) added 2 buttons and 2 textboxes. My approach is very simple. Have a look at the Control.Leave[^] event. I use this event to register the last focused textbox, caret position in it and the length of the selected text. After a button click occurs I restore the selection in the textbox and then replace the selected text. This way you can add text to cursor position and/or replace selected text. Examine the source above. I believe that this approach is easy to understand and use. Give it a try and let me know if there are some problems. :)
[/UPDATE 2.1]


您可以在表单的Load事件或表单的构造函数中使用以下内容:
You can have something like this in the Load event of the form or in the constructor of the form:
class Form1{
TextBox _activeTextBox = null;

        public Form1()
        {
            InitializeComponent();
            BindGotFocusHandler();
        }

        private void BindGotFocusHandler()
        {
            foreach (Control control in this.Controls)
            {
                if (control.Controls != null && control.Controls.Count > 0)
                {
                    BindGotFocusHandler();
                }

                control.GotFocus += new EventHandler(ControlFocussed);
            }
        }

        private void ControlFocussed(object sender, EventArgs e)
        {
            TextBox textBox;
            if ((textBox = sender as TextBox) != null)
            {
                _activeTextBox = textBox;
            }
            else
            {
                _activeTextBox = null;
            }
        }

        private void KeyBoardButtonClick(object sender, EventArgs e)
        {
            if (_activeTextBox != null)
            {
                _activeTextBox.AppendText("button character");
            }
        }
}


我建​​议您使用Enter事件获取集中控件(不仅是文本框),然后将集中控件传递给触摸屏键盘控件.单击触摸屏控件中的按钮时,您无需在文本框的内容后附加文本,这是一个更好的方法.您可以使用SendKeys发送适当的密钥.就像有人按下键一样.
另一个想法:不要为每个按钮创建自定义事件处理程序,只需创建一个从Button继承来代表字母的用户控件(例如TouchscreenKeyButton)并添加"KeyValue"属性.使用该属性来确定按下的键.例如:代表A字母的TouchscreenKeyButton在KeyValue属性中应具有"a"值.当发生点击事件时,只需使用SendKeys发送此字母即可.注意Capslock和移位.但是我认为必须由键盘来管理,而不是键.
希望很快能收到您的来信.
I would recommend you to get the focussed control (not only textboxs) using the Enter event and then pass the focussed control to the touchscreen keyboard control. When a button in the touchscreen control is clicked you don''t need to append a text to the content of the textbox, there''s a better aproach. You can use SendKeys to send the appropiate key. It''s like if someone were pressing the key.
Another idea: don''t make a custom event handler for every button, just create a user control that inherits from Button to represent a letter (let''s say TouchscreenKeyButton) and add a "KeyValue" property. Use that property to determine the key pressed. For example: TouchscreenKeyButton representing the A letter should have a ''a'' value in the KeyValue property. When click event occurs, just use SendKeys to send this letter. Be careful with Capslock and shift. But in my opinion that has to be managed by the keyboard, not the key.
I hope to hear from you soon.


这篇关于键盘功能C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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