使用SetKeyDelay,0、0时,ControlSend随机发送错误的字符(已修改和未修改) [英] ControlSend randomly sending wrong characters (modified and unmodified) when using SetKeyDelay, 0, 0

查看:178
本文介绍了使用SetKeyDelay,0、0时,ControlSend随机发送错误的字符(已修改和未修改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自我回答这个问题,因为我已经看到它遍及整个互联网,但是答案很少,而且在堆栈溢出方面绝对找不到解决方法.

I'm self-answering this question because I've seen it asked all over the Internet, but with few helpful answers, and definitely no resolutions on Stack Overflow that I can find.

考虑以下代码,该代码只需编写几行shell命令:

Consider this code, which simply writes several lines of shell commands:

^0::
    SetKeyDelay, 0, 0
    myWindow = ahk_exe Notepad.exe
    ControlSend, , set c=".cshrc-andrew.cheong"`n, %myWindow%
    ControlSend, , set v=".vimrc-andrew.cheong"`n, %myWindow%
    ControlSend, , foreach d ( /userhome/andrew.cheong /home/$USER /data/$USER )`n, %myWindow%
    ControlSend, , if ( -e $d/$c ) source $d/$c`n, %myWindow%
    ControlSend, , if ( -e $d/$v ) alias vim "vim -N -u $d/$v"`n, %myWindow%
    ControlSend, , end`n, %myWindow%
    Return

我正在将命令编写到记事本中,以表明这不是不是问题,仅限于终端应用程序(例如PuTTy或xterm).很容易想到,因为这些应用程序有时会出现滞后的行为.

I'm writing the commands to Notepad to show that it is not an issue limited to terminal applications like PuTTy or xterm. It's easy to think so, since these applications sometimes have laggy behavior.

特别是在使用SetKeyDelay, 0, 0进行快速键入"时,我会遇到各种奇怪的行为,例如:

Specifically when using SetKeyDelay, 0, 0 for fast "typing," I get all kinds of weird behavior, like:

  • 双引号替换为单引号.括号用9和0代替.美元符号替换为4.

  • Double-quotes replaced by single-quotes. Parentheses replaced by 9's and 0's. Dollar signs replaced by 4's.

set c=".cshrc-andrew.cheong'
set v=".vimrc-andrew.cheong"
foreach d ( /userhome/andrew.cheong /home/$USER /data/$USER )
if 9 -e 4d/4c 0 source 4d/4c
if ( -e $d/$v ) alias vim 'vim -n -u 4d/4v'
end

  • 用下划线代替短划线.大小写更改.

  • Dashes replaced by underscores. Case changes.

    set c='.cshrc-andrew.cheong"
    set v='.vimrc-andrew.cheong'
    foreach d ( /userhome/andrew.cheong /home/4user /data/$USER )
    if 9 -e 4d/$C ) source 4d/$c
    if 9 _e $d/$v ) alias vim 'vim -N -u $d/$v"
    end
    

  • 句号由>代替.更多案例更改.

  • Periods replaced by >'s. More case changes.

    set c=".cshrc-andrew.cheong"
    set v=">VIMrc-andrew.cheong"
    foreach d ( /userhome/andrew.cheong /home/$USER /data/$USER )
    if 9 -e $d/$c ) source 4d/$c
    if ( -e $d/$V ) alias vim "vim -N -u $d/$v"
    end
    

  • 很明显,此问题与 Shift 修饰符有关,就像它是随机打开还是关闭一样.

    It's obvious the issue has something to do with the Shift modifier, as if it's being randomly turned on or off.

    为什么会这样,我们该如何解决?

    请注意,使用Send及其变体没有问题.该问题特别出现在ControlSend上,需要将输入发送到特定控件或未聚焦的窗口.

    Note that there are no problems when using Send and its variants. The issue specifically arises with ControlSend, which is needed to send inputs to specific controls or to an unfocused window.

    推荐答案

    解决方案

    将其复制到脚本中(从AutoHotkey论坛中的RHCP用户):

    Solution

    Copy this into your script (from user RHCP at AutoHotkey forums):

    pSendChars(string, control := "", WinTitle := "", WinText := "", ExcludeTitle := "", ExcludeText := "")
    {
        for k, char in StrSplit(string)
            postmessage, WM_CHAR := 0x102, Asc(char),, %Control%, %WinTitle%, %WinText%, %ExcludeTitle%, %ExcludeText%
        return
    }
    

    并像这样使用它:

    ^0::
        myWindow = ahk_exe Notepad.exe
        line .= "set c="".cshrc-acheong""`n"
        line .= "set v="".vimrc-acheong""`n"
        line .= "foreach d ( /userhome/andrew.cheong /home/cama /home/$USER )`n"
        line .= "    if ( -e $d/$c ) source $d/$c`n"
        line .= "    if ( -e $d/$v ) alias vim ""vim -N -u $d/$v""`n"
        line .= "end`n"
        pSendChars(line, "edit1", myWindow)
        Return
    

    就是这样.

    请注意,edit1是其文本控件的记事本名称.当将此脚本用于PuTTy时,我将其更改为空白字符串.使用AutoHotkey的WindowSpy程序找出要写入的控件.

    Note that edit1 is Notepad's name for its text control. When using this script for PuTTy, I changed it to a blank string. Use AutoHotkey's WindowSpy program to find out the control you wish to write to.

    这已经出现过几次了.只是添加到Lexikos 写道,该问题是由于controlSend同时使用两个帖子 message和sendInput/sendEvent发送击键.这是必需的 因为某些程序无法正确解释按键,除非 发送的修饰键在逻辑上是向下的(通过sendInput/Event发送).

    This has come up a few times before. Just to add to what Lexikos wrote, the issue is due to the fact that controlSend uses both post message and sendInput/sendEvent to send keystrokes. This is required as some programs will not correctly interpret the keystrokes unless the sent modifier keys are logically down (sent via sendInput/Event).

    修饰符键(除非有明确说明,例如{shitft down})已发送 通过sendInput,而非修饰符通过postMessage发送.按键已发送 通过后消息直接发送到窗口,因此延迟更少 而不是其他按键/消息.因此,可以进行击键 消息不同步到达,导致出现意外字符.在 您的情况大写字母要求通过Shift键发送 sendInput/Event.

    Modifier keys (unless explicitly stated e.g. {shitft down}) are sent via sendInput while non-modifers are sent via postMessage. Keys sent via postmessage are sent directly to the window and so have less delay than the other keys/messages. Consequently it's possible for keystroke messages to arrive out of synch resulting in unexpected characters. In your case the capitalised letters require the shift key to be sent via sendInput/Event.

    除了使用keyDelays之外,您还可以尝试controlSetText并发布 WM_Char消息.如果您正在使用文本控件,我会 建议使用controlSetText.

    In addition to using keyDelays, you can try controlSetText and posting WM_Char messages. If you're working with a text control i would recommend using controlSetText.

    - 2013年9月30日的RHCP

    这篇关于使用SetKeyDelay,0、0时,ControlSend随机发送错误的字符(已修改和未修改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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