将循环中的键发送到活动表单 [英] Send keys in a Loop to the active form

查看:80
本文介绍了将循环中的键发送到活动表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



有没有一种简单的方法来填写实际形式的东西?

例如我在编辑器并按下一个随机定义的键来启动程序..

程序得到一个初始值(1)并将循环中的值更改为10,这应该是插入到当前游标位置。



所以如果我定义一个初始值为1并循环到10,我的.txt文件应该最终看起来像



// ------ BOF

1

2

3

4

5

6

7

8

9

10

// ----- EOF



谢谢你们!



*编辑*

我需要知道的是如何按F2启动我的功能?

Hey guys,

is there a easy way to fill something in the actual form I am?
for example I am in the editor and press a random defined key to start the programm..
the programm got an initial value (1) and I change this value in a loop to 10 and this should be insertet to the current cursor location.

so if I define an initial value of 1 and loop till 10 my .txt file should finally look like

//------ BOF
1
2
3
4
5
6
7
8
9
10
//----- EOF

thank you guys !

*edited*
all I need to know is how can I start my function with pressing F2?

推荐答案

将以下代码添加到表单加载事件:

Add the code following code to your form load event:
this.KeyUp += new System.Windows.Forms.KeyEventHandler(OnKeyPress);



现在将表单的KeyPreview属性设置为true


Now set your form''s KeyPreview property to true

private void OnKeyPress(object sender, KeyEventArgs e)
{
   
    if (e.KeyCode  == Keys.F2)
    {
        //call your function here.
    }
}


请看我对这个问题的评论并考虑一下。



有一种在低级别模拟用户输入的通用方法:Windows API SendInput

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310% 28v = vs.85%29.aspx [ ^ ]。



这是在.NET中调用此方法的方法:

http:// pinvoke。 net / default.aspx / user32.sendinput [ ^ ]。



这有一个问题:它会打破.NET代码的平台可移植性,只能在Windows上运行。



使用 System.Windows.Forms ,有一件事, System.Windows.Forms.SendKeys http://msdn.microsoft.com/en- us / library / system.windows.forms.sendkeys.aspx [ ^ ]。



这件事实际上只适用于表格;它的可用性非常有限,以至于我不确定它是否对任何东西都有好处。



但首先,考虑放弃它的想法模拟用户输入。在绝大多数情况下,这是最好的方法。 :-)



-SA
Please see my comment to the question and think about it.

There is one universal way to simulate user input on a low level: Windows API SendInput:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

This is how to call this method in .NET:
http://pinvoke.net/default.aspx/user32.sendinput[^].

There is one problem with this: it will break platform portability of your .NET code, will work only on Windows.

With System.Windows.Forms, there is one thing, System.Windows.Forms.SendKeys, http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx[^].

This thing is really only for Forms; and its usability is so limited that I''m not sure if it is good for anything at all.

But first of all, think about giving up the idea of simulating user input. In absolute majority of cases this is the very best approach. :-)

—SA


这篇关于将循环中的键发送到活动表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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