C#键盘输入 [英] c# keyboard input

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

问题描述

我是C#的新手,并且不熟悉OOP.

我正在将TurboC程序转换为Windows.

旧程序使用全局变量来存储用户密钥.
C#prog具有用于同一目的的静态变量.
这由重写的OnKeyDown更新.

但是,在输入通过表单菜单调用的方法时,按键将被忽略.

我担心这对我的工作方式很重要.
怀疑没有从过程编程中抓住变化.

谁能指出我正确的方向.

感谢

I am new to C# and not up to speed with OOP.

I am converting a TurboC program to Windows.

The old program used a global variable to store user key srokes.
The C# prog has a static variable for the same purpose.
This is updated by an overriden OnKeyDown.

However on entering a method, called via a form menu, key strokes are ignored.

I fear it is something fundamental in my approach.
Suspect not grasping change from procedural programming.

Can anyone point me in the right direction.

Thanks

推荐答案

我认为您需要将KeyPreview属性设置为True才能获取所有击键.

http://msdn.microsoft.com/en-us/library/system. windows.forms.form.aspx [ ^ ]

如果您的意思是您的方法执行了相当长的时间,那么这意味着不会处理用于处理应用程序消息(例如按键)的消息循环.如果要花很长时间,您还将在任务管理器中获得(不响应)".

您可以在线程中执行您的方法.最基本的方法是:
I think you need to set the KeyPreview property to True to get all keystrokes.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^]

If you mean that your method executes for quite some time, then this means that the message loop that deals with application messages (like a keystroke) isn''t handled. If it would take to long, you would also get the "(not responding)" in the taskmanager.

You could execute your method in a thread. The most basic way to do this is:
using System.Threading;
// ...
new Thread (new ThreadStart (SomeObject.SomeMethod)).Start();



上面的代码未维护对线程的引用,但这只是一个简单的示例.

这是一些额外的参考:
http://www.yoda.arachsys.com/csharp/threads/ [ http://www.yoda.arachsys.com/csharp/threadstart.html [ ^ ]

同样重要的是要知道要从线程进行屏幕更新,必须使用Invoke:
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx [ ^ ]

有关如何控制线程的一些信息:
http://msdn.microsoft.com/en-us/library/7a2f3ay4% 28VS.80%29.aspx [ ^ ]

您也可以在没有线程的情况下使用过多的Application.DoEvents(); 来膨胀方法,以确保处理消息循环.但是,这非常危险且容易出错.每个保存人员都会建议您不要使用此骇客...因此,请注意!

祝你好运!



The code above doesn''t maintain a reference to the thread, but it''s just a simple example.

Here is some extra reference:
http://www.yoda.arachsys.com/csharp/threads/[^]

http://www.yoda.arachsys.com/csharp/threadstart.html[^]

It is also important to know that Invoke must be used if you would like to do some screen updating from a thread:
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx[^]

Some info on how to control the thread:
http://msdn.microsoft.com/en-us/library/7a2f3ay4%28VS.80%29.aspx[^]

You could also do without threads and bloat your methods with an excessive amount of Application.DoEvents(); to ensure the message loop is handled. This is however quite dangerous and error prone. Every save person will advise against the use of this hack... so, be warned!

Good luck!


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

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