SendKeys并不总是有效 [英] SendKeys doesn't always work

查看:58
本文介绍了SendKeys并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Excel VBA中使用 .SendKeys()将击键发送到我正在使用shell .AppActive 方法激活的外部窗口.问题在于 SendKeys 的行为根本不一致,有时会发送密钥,有时却发送密钥.我认为这与根据MSDN文档将密钥存储在缓冲区中有关.

I am using .SendKeys() in Excel VBA to send key strokes to an external window that I am making active using shell .AppActive method. The problem is that SendKeys is simply not behaving consistently and is sometimes sending the keys and sometimes not. I think it has something to do with storing the keys in the buffer according to MSDN documentation.

如何解决这个问题?

推荐答案

我发现此问题的解决方案是使应用程序暂停一会儿.这似乎允许清除缓冲区(如果我错了,请纠正我).

I found that the solution to this problem was to just make the application pause for a short while. This seems to allow the buffer to clear (correct me if I'm wrong please).

为了使Excel VBA进入睡眠状态,我通过在模块顶部进行编写来公开了Windows API函数 Sleep :

In order to make Excel VBA sleep, I exposed the windows API function Sleep by writing at the top of a module:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

然后在每个 SendKeys 命令之后,我只是使用:

And then after every SendKeys command, I just used:

Sleep 1

从字面上看,所有差异均以1毫秒为单位,并且密钥始终正确发送.

And literally, 1 millisecond made all the difference and the keys were always sent correctly.

这篇关于SendKeys并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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