如何将sendkeys pgup/pgdn带到前景文本框
我正在尝试以Net 6.0上简单的Windows表单上的简单文本框控件的内容进行分页/向下分页,但有些不对劲。启用了文本框快捷方式(可能是为什么sendkeys.sendwait(“^a”);
works)而ReadOnly是错误的。
我有一个方法(不是在UI线程上),我称之为sendkeys.sendwait(“ {pgup}”);
to to to toepground应用程序(这既是密钥发件人又是textbox(focus) 。
- 接收器
如果i
sendkeys.sendwait(“^a”);
,文本框按预期选择所有文本。如果i
sendkeys.sendwait(“ {pgup}”);
,文本框在文本底部添加了一个空白。
由此我得出的结论是,我的代码正常工作,因为它发送了“^a”,并且文本框接收并选择所有文本。但是,即使在键盘发送PGUP键时,文本框也无法处理“ {pgup}”键。
我很容易阅读网络上的十二篇文章和帖子,因此谈论使用滚动事件进行分页,将其定位,然后滚动到Caret,等等。但是,他们都没有关于为什么sendkeys(^a)和键盘PGUP会起作用,但是sendkeys.sendwait(“ {pgup}”)会失败。
谁能告诉我我在做什么错,也许我需要做(或阅读)修复它?谢谢
更新:Jimi要求提供一些代码,因此这是我用来发送 ^a和{pgup}键的代码。我知道这不在UI线程上,因为它是从语音驱动识别器线程执行的。该应用程序是语音驱动的应用程序,该应用程序通过TextBox.Appendlines调用在文本框中显示内容。我也试图通过语音来pgup和pgdn多行文本框。
当我尝试使用发送时(我通常使用.sendwait用于其他程序中的所有内容),我收到以下错误消息:
system.invalidoperationException:'sendkeys无法在此内运行 应用程序是因为应用程序没有处理Windows消息。 更改应用程序以处理消息,或者使用 sendkeys.sendwait方法。'
的确,我的应用不会拦截Windows消息。我无法弄清楚该应用程序为什么可以接收并正确处理我的键盘键,而我的“^a''快捷键,而不是sendwait(“ {pgup}”)键。
internal static void
HelperPageUp() {
var keys = "{PgUp}";
keys = "^a";
SendKeys.SendWait(keys);
}
我开始认为{pgup}从来没有通过文本框或控制来处理,可能会在logic中使用逻辑来处理PGUP“订单”的案例语句,以将PGUP实现为接收PGUP密钥的应用程序我将不得不在表格中添加一个键处理程序。
textBox1_KeyUp(object sender, KeyEventArgs e) {
// identify the special key and implement what it means
if (e.KeyCode == Keys.PageDown) {
...
e.Handled = true;
}
I am trying to page up/down the contents of a simple textbox control in a simple Windows Form on NET 6.0, but something is wrong. Textbox shortcuts are enabled (probably why SendKeys.SendWait("^a");
works) and readonly is false.
I have a method (not on the UI thread) that I call to SendKeys.SendWait("{PgUp}");
to the foreground app (which is both the key sender and textbox (with focus) receiver.
If I type PgUp on the keyboard, the textbox pages up as expected.
If I
SendKeys.SendWait("^a");
, the textbox selects all text as expected.If I
Sendkeys.SendWait("{PgUp}");
, the textbox adds a blank line to the bottom of the text.
From this I conclude that my code is working because it sends "^a" and the textbox receives it and selects all text. But somehow the textbox does not handle the "{PgUp}" key, even though it does when the PgUp key is sent by the keyboard.
I've read easily a dozen articles and posts on the web and SO that talk about paging using scrolling events, positioning the caret and then scrolling to the caret, and so on. But none of them say anything about why SendKeys(^a) and keyboard PgUp would work but SendKeys.SendWait("{PgUp}") would fail.
Can anyone tell me what I'm doing wrong and maybe what I need to do (or read) to fix it? Thank you
UPDATE: Jimi asked for some code, so here is the code that I use to send the ^a and the {PgUp} keys. I know this is not on the UI thread because it is executed from a voice-driven recognizer thread. The app is a voice-driven app that displays content in the textbox by textbox.AppendLines calls. I was trying to PgUp and PgDn the multi-line textbox by voice as well.
When I tried to use Send (I normally use .SendWait for everything in other programs), I received the following error message:
System.InvalidOperationException: 'SendKeys cannot run inside this
application because the application is not handling Windows messages.
Either change the application to handle messages, or use the
SendKeys.SendWait method.'
It is true that my app does not intercept Windows messages. I can't figure out why the app can receive and properly process my keyboard keys, and my "^a' shortcut keys, but not the SendWait("{PgUp}") key.
internal static void
HelperPageUp() {
var keys = "{PgUp}";
keys = "^a";
SendKeys.SendWait(keys);
}
I'm starting to think that {PgUp} is never handled by a textbox or control. Instead, probably {PgUp} must be handled by logic in a case statement that converts PgUp "orders" into sets of actions that implement whatever PgUp means to the app that receives the PgUp key. So maybe I will have to add a keystroke handler to the form. Maybe something like this:
textBox1_KeyUp(object sender, KeyEventArgs e) {
// identify the special key and implement what it means
if (e.KeyCode == Keys.PageDown) {
...
e.Handled = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我在问题结束时的想法是正确的。 ^a是由文本框处理的,因为我有
textbox.enableShortCuts = true;
,因此文本框处理了流行的 ^快捷方式。但是像{pgup}这样的键是另一回事。它们不包括在捷径中。解决方案是编写代码以在形式中明确处理{pGup}键。这是我有效的代码。
Yes, my thought at the end of the question was correct. The ^a was handled by the textbox because I had
textbox.EnableShortcuts=true;
, so the textbox handled the popular ^a shortcut. But keys like {PgUp} are a different matter; they are not included in shortcuts.The solution was to write code to handle the {PgUp} key explicitly in the form. Here is my code that worked.