0

我无法使用 xdotool 在浏览器中模拟简单的按键。

现在我的浏览器通过在'/home/pi/.xintirc'中添加以下代码在启动时启动

#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL]
python /etc/xdo_test.py

我的 /etc/xdo_test.py 如下所示:

import time
import subprocess

time.sleep(20)
subprocess.call(["xdotool", "key", "c"]);

在启动时使用此文件时我没有任何输出,但如果我在另一个控制台中执行此操作,我会得到以下输出:

Error: Can't open display: (null)
Failed creating new xdo instance

有谁知道我为什么会收到此错误以及如何解决它?

4

3 回答 3

0

您在 python 脚本中使用该subprocess.call命令。此调用不会在子流程中设置当前设置的环境变量。因此缺少显示。只需直接在.xinitrc文件中调用 xdotool 命令即可。


#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL] &           #<--- add ampersand
sleep 20
# you have to determine the window to which you want to send the keystroke
WIN=`xdotool search --sync --onlyvisible --class iceweasel | head -1`
# then activate it
xdotool windowactivate $WIN
# and send the keystroke
xdotool key --window $WIN c

如果iceweasel调用中的 & 符号有问题,请尝试在 URL 周围加上引号。

于 2015-05-18T15:26:56.633 回答
0

我让它工作。我最终找到了这个教程并使用了其中的一些想法。我会为可能有类似问题的人发布解决方案。

这是我放在 /home/pi/.xinitrc 文件中的内容:

#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL] &
sudo /etc/xdo_test.sh

我将 python 脚本更改为 shell 脚本,并插入了以下代码:

sleep 20
$WIN=$(xdotool search --onlyvisible --class Iceweasel|head -1)
xdotool key --window $WIN c

while:
do
    sleep 300
done

最后的 while 循环是我添加的,因为 Xserver 从失去与脚本的连接的那一刻起就崩溃了。我仍在寻找一个更干净的解决方案来结束脚本,但这目前有效。当我找到一个时,我会更新这个 awnser。

感谢塞巴斯蒂安斯蒂格勒的帮助!

于 2015-05-25T10:32:12.987 回答
0

在运行窗口管理器之前调用 xdo_test.sh

于 2016-03-24T18:58:26.477 回答