TextBox如何从按键输入中获取文本? (基本TextBox方法) [英] How does TextBox get text from key input? (Basic TextBox Method)

查看:139
本文介绍了TextBox如何从按键输入中获取文本? (基本TextBox方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道它是如何完成的,我在msdn上询问并查看了TextBox代码,并完全迷失了,所以我决定在这里问一下:)
我只想写一个像这样的方法

So I''m wondering how it is done, I asked on msdn and viewed the TextBox code and got completely lost so I decided to kindly ask here :)
I simply want to write a method that looks like that

char GetChar(int keycode, bool shiftState, object something/*Arabic(101), Arabic(102), Albanian, English(US), English(UK)...etc CultureInfo maybe*/)
{
    char output = ''\0'';
    bool capsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
    //Some code....
    return output;
}

推荐答案

一个TextBox(假设您的意思是System.Windows.Forms.TextBox-如果没有,请具体说明)实际上是Win32 Edit公用控件的包装,因此实际上会将文本发布到窗口句柄(HWND),而Windows中的本机代码则负责处理.

但是TextBox可以通过重写OnKeyDown处理输入(或如果不想继承子类,则可以处理KeyDown事件).

但是,从您的问题中我不清楚您要做什么.如果我们能理解您要尝试做的事情(而不仅仅是一个关于先入为主的问题的问题),那么我们可以更好地提出一个解决方案.
A TextBox (assuming you mean System.Windows.Forms.TextBox - please be specific if not) is actually a wrapper around the Win32 Edit common control, so text is actually posted to a window handle (HWND) and native code in Windows handles processing.

But a TextBox can handle input by overriding OnKeyDown (or handling the KeyDown event if you don''t want to subclass).

However, I''m not clear from your question what it is you''re trying to do. If we could understand what you''re trying to do (and not just a question on a preconceived how) we could better propose a solution.


您应该处理事件.该事件的事件参数将为您提供按下的键字符;您可以将其与期望输入的Unicode代码点范围进行比较,并发出警告.另外(或更好),您可以过滤掉不需要的字符,因为可以取消此事件.使用属性Canceltrue的值(如果事件参数为参数,则为属性).

—SA
You should do it handling the event KeyPress. The event arguments of this event will give you the key character pressed; you can compare it with the Unicode code point ranges you expect for input and issue the warning. Alternatively (or better additionally), you can filter out unwanted characters as this event can be cancelled; use the value of property Cancel to true (this is the property if the event arguments parameter).

—SA


在前面的讨论中,我认为这是您需要的:
InputLanguage.CurrentInputLanguage [ InputLanguageCollection [
From the previous discussion I think this is what you need:
InputLanguage.CurrentInputLanguage[^] - this property gets or sets the input language for the current thread.

Use InputLanguageCollection[^] as the source of available input languages.

Best regards
Espen Harlinn


这篇关于TextBox如何从按键输入中获取文本? (基本TextBox方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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