我的R脚本如何检查在Windows下哪个窗口是活动的?

huangapple go评论72阅读模式
英文:

How can my R script check which window is active under windows?

问题

我正在尝试使用KeyboardSimulator包来自动化某些操作。

我想在R中使用键盘模拟器包来按下"alt+tab",然后检查窗口是否已打开,然后再继续。然后脚本可以安全地按"tab",直到找到正确的文本输入框。

我知道我可以使用

getWindowsHandles(which = "all")

来检查所有打开窗口的名称,但是否有一种方法可以检查哪个窗口是活动窗口?

此外,是否有一种方法可以检查文本框的名称?

英文:

I am trying to use the KeyboardSimulator package to automate something.

I would like to use the keyboard simulator package in R to hit "alt+tab" and then check if a window is open before continuing. Then it is safe for the script to hit "tab" until it hits the right text input box.

I know I can use

getWindowsHandles(which = "all")

to check the name of all open windows, but is there a way to check which window is active?

Furtheremore, is there a way to check the name of the text box?

答案1

得分: 4

以下是翻译好的部分:

假设您知道要通过 library(KeyboardSimulator) 操作的窗口的句柄名称,这里有一个解决方法:

以下代码将最小化所有已打开的窗口,然后只还原感兴趣的窗口(在此示例中为 notepad++),使其成为活动窗口:

library(KeyboardSimulator)

system2("C:\\Program Files (x86)\\Notepad++\\notepad++.exe", wait = FALSE) # 异步运行notepad++
Sys.sleep(2) # 等待notepad++窗口打开(仅用于演示,也可以手动打开)
all_handles <- getWindowsHandles(which = "all", minimized = TRUE)
notepad_handle <- all_handles[grep("notepad", names(all_handles), ignore.case = TRUE)[1]] # 查找notepad++的句柄并使用第一个匹配项

if (is.null(notepad_handle[[1]])) {
  message("No 'notepad' handle available")
} else {
  arrangeWindows(action = "minimize", windows = all_handles) # 最小化所有窗口
  arrangeWindows(action = "restore", windows = notepad_handle) # 还原notepad++句柄

  # 在活动窗口上使用library(KeyboardSimulator)
  keybd.type_string("test")
  keybd.press('Tab')
  keybd.type_string("test")
}

希望这对您有所帮助。如果您需要进一步的帮助,请告诉我。

英文:

Assuming you do know the handle name of the window you are planning to manipulate via library(KeyboardSimulator) here is a workaround:

The following minimizes all opened windows and after that only restores the window of interest (here notepad++) so it is the active one:

library(KeyboardSimulator)

system2(&quot;C:\\Program Files (x86)\\Notepad++\\notepad++.exe&quot;, wait = FALSE) # run notepad++ asynchronously
Sys.sleep(2) # wait for notepad++ window to be opened (for demo only - can be opened manually)
all_handles &lt;- getWindowsHandles(which = &quot;all&quot;, minimized = TRUE)
notepad_handle &lt;- all_handles[grep(&quot;notepad&quot;, names(all_handles), ignore.case = TRUE)[1]] # search notepad++ handle and use the first match

if(is.null(notepad_handle[[1]])){
  message(&quot;No &#39;notepad&#39; handle available&quot;)
} else {
  arrangeWindows(action = &quot;minimize&quot;, windows = all_handles) # minimize all windows
  arrangeWindows(action = &quot;restore&quot;, windows = notepad_handle) # restore notepad++ handle
  
  # use library(KeyboardSimulator) on the active window
  keybd.type_string(&quot;test&quot;)
  keybd.press(&#39;Tab&#39;)
  keybd.type_string(&quot;test&quot;)
}

huangapple
  • 本文由 发表于 2023年2月26日 21:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定