【问题标题】:How to get mouse position properly on keypress with autohotkey Invalid Hotkey Error如何使用自动热键无效热键错误在按键上正确获取鼠标位置
【发布时间】:2021-05-12 20:41:46
【问题描述】:

对于那些后来发现这个的人,出于谷歌原因或任何其他原因,虽然 cmets 提供了帮助,但我在很大程度上不得不从头开始重新编写代码。如果你想在 Procreate 中使用相当于私有层的 CSP,或者如果你想在不显示参考层的情况下使用 CSP 录制功能,请下载 autohotkey 和此脚本,然后进行以下设置。

  1. CTRL 和 Numpad+ 放大
  2. CTRL 和 Numpad- 缩小
  3. 鼠标中键平移。

然后执行以下操作,在两页上放置某种类型的引用,一个大的棋盘格,一个有几行的点,等等。然后将两个窗口对齐,使它们显示完全相同的内容,仅使用鼠标中键进行平移,使用 CTRL+ 和 CTRL- 进行放大和缩小。

这是我的第一个自动热键脚本,我正在尝试链接艺术程序的两个不同窗口,这样当我平移一个画布时,它会平移另一个画布。我希望它尽可能精确,但是我在第 1 行收到一个错误,说它是无效的热键,我尝试删除变量并移动逗号,但我不确定是什么我做错了。

这是我的代码。出于目的,我将解释按钮组合。

Space + L Mouse Button 一起让我可以平移画布,当我释放一个或另一个时,我需要知道光标在哪里。

LCtrl + Numpad5 禁用在 WindowTop 上的单击,我用它来将一个窗口覆盖在另一个窗口之上。

LCtrl + Tab 在应用中的两个画布之间切换

#IfWinActive ahk_exe CLIPStudioPaint.exe

; coordmode 
Coordmode, Mouse, Screen 
t := 0

;Below line starts script, and declares The Starting Coordinates

WinTitle = 3DLayer
SetTitleMatchMode, 2

^F9::
WinSet,Transparent, Off

^F11::
WinSet, ExStyle, -0x20, % WinTitle
return

^F10::
WinSet, ExStyle, +0x20, % WinTitle
WinSet, Transparent, 128, % WinTitle
return

~MButton::
 if (t = 0)
{
    MouseGetPos, MouseStartX, MouseStartY
    t := 1
}

return

~MButton up:: 

;When MButton is released, records mouse ending positions
    MouseGetpos, MouseEndX, MouseEndY
;Blocks input in case I move mouse or touch keyboard while it's running
    BlockInput, On
; switches canvas
    Send ^{TAB} 
; set window for click through
    WinTitle = 3DLayer
    SetTitleMatchMode, 2
; Disables Click Through
    WinSet, ExStyle, -0x20, % WinTitle
        ; drag 
    MouseClickDrag, M, MouseStartX, MouseStartY, MouseEndX, MouseEndY
;Re-enables Click Through
    WinSet, ExStyle, +0x20, % WinTitle
    Winset, Transparent, 150, % WinTitle
;Turns inputs back on, so that I can resume using the art program
    Send ^{TAB}
    BlockInput, Off
    t := 0
return

~^NumpadAdd::
    BlockInput, On
; Get Mouse Position
    MouseGetPos, ScrollX, ScrollY

;Block input, then switch canvases
    
    Send ^{Tab}
sleep 50
; Disables Click Through
    WinSet, ExStyle, -0x20, % WinTitle
; Move Mouse where it needs to be then scroll
    MouseMove, ScrollX, ScrollY
    Send ^{NumpadAdd}
;Re-enables Click Through
    WinSet, ExStyle, +0x20, % WinTitle
    Winset, Transparent, 150, % WinTitle
;Turns inputs back on, so that I can resume using the art program
    Send ^{TAB}
    BlockInput, Off
return

~^NumpadSub::
    BlockInput, On
; Get Mouse Position
    MouseGetPos, ScrollX, ScrollY

;Block input, then switch canvases
    
    Send ^{Tab}
sleep 50
; Disables Click Through
    WinSet, ExStyle, -0x20, % WinTitle
; Move Mouse where it needs to be then scroll
    MouseMove, ScrollX, ScrollY
    Send ^{NumpadSub}
;Re-enables Click Through
    WinSet, ExStyle, +0x20, % WinTitle
    Winset, Transparent, 150, % WinTitle
;Turns inputs back on, so that I can resume using the art program
    Send ^{TAB}
    BlockInput, Off
return

【问题讨论】:

  • 老实说,如果你不这么说,我真的无法判断这是 AHK。这与功能性 AHK 代码相差甚远。您能否尝试更好地解释每行应该做什么,所以也许可以采取一些措施来纠正它们。根据您的描述,我真的无法判断脚本应该做什么。
  • 我已经添加了 cmets 来说明页面上每一行的作用
  • 我同意@0x464e 对您的代码的评估——您使用的许多语法类似于非AHK 语言。这里有一些你可以做的更简单的修改来修复它: 1. 当你试图给变量赋值时,使用:= 而不是=; 2. AHK中的单行cmets以;开头(像Python一样),而不是//(像Java、C++等使用); 3. 如果您想让 AHK 将击键发送到您的计算机,请查看Send 命令。这是The Tutorial AHK 的快速入门
  • 感谢您链接我的教程,并帮助解读我正在阅读的内容,它说我可以使用 = 命令代替 := 命令,它只是一个遗留功能,它还说当我同时按下两个键来发送命令时,我使用 & 符号,最后它说我通过将变量列为 theVariableName := variableValue 来声明一个变量。在词汇表中,它说我可以按照我这样做的方式为 MouseGetPos 的变量赋值。您能否指出哪些具体部分不正确?我确实错过了我需要使用发送命令,但在那之前它就坏了。
  • 我知道@AHK_Questionnaire,这只是我注意到的许多更大问题的快速解决方案,因为当时其他承诺使我无法给出完整的答案。如果我今天不能完成,我会尝试今晚(时间允许)和明天晚上试一试

标签: automation autohotkey


【解决方案1】:

这个时候,我做的最大的改变就是替换

Space UP || LMButton UP::MouseGetPos MouseEndX, MouseEndY

while (GetKeyState("LButton", "P") and GetKeyState("Space", "P")) 
    sleep 50  
 
MouseGetPos MouseEndX, MouseEndY

(因为你不能有嵌套的热键)


我实际上不知道它是否按预期工作,因为我没有您正在使用的软件,但如果有什么不正常工作,请随时 lmk。


更新 1:

为了保留键的原生功能,我们可以使用~ 修饰符。

来自modifier section of the docs的相关部分:

当热键触发时,其键的本机功能不会被阻止 (对系统隐藏)。

为了实现这一点,我们可以将~键作为前缀添加到热键中,转

Space & LButton::

进入

~Space & LButton::

当前代码 v2:

#IfWinActive ahk_exe CLIPStudioPaint.exe
;Below line starts script, and declares 4 variables with the value 0 to reset the script, to make sure for no reason it uses the old values
~Space & LButton::
;Once variables are declared, it records those variables
MouseGetPos MouseStartX, MouseStartY

;When space or Mouse button release, it uses the 4 recorded variables to move and click on the canvas

while (GetKeyState("LButton", "P") and GetKeyState("Space", "P")) 
    sleep 50  
 
MouseGetPos MouseEndX, MouseEndY

;Blocks input in case I move mouse or touch keyboard while it's running
BlockInput, On

;Disables Click Through in WindowTop (That's a literal name of the program) by pressing CTRL and Numpad5
Send ^{Numpad5}

;Switches Canvasses to the new canvas (Basically a 2nd tab in the program)
Send ^{TAB}

;Uses the recorded mouse positions to middle mouse button drag the exact same way I had in the previous section
MouseClickDrag, M, MouseStartX, MouseStartY, MouseEndX, MouseEndY

;Re-enables Click Through
Send ^{TAB}

;Switches back too original canvas
Send ^{Numpad5}

;Turns inputs back on, so that I can resume using the art program
BlockInput, Off

return
Space::Space

【讨论】:

  • 这几乎是完美的,我遇到的唯一问题是当宏激活时,空格键和左键没有保持不动,因此程序没有读取我正在拖动的内容。
  • 这应该很容易解决,如果您希望热键中的键保留其本机功能,并且不被阻止(对系统隐藏),您可以使用~ 修饰符。我将更新帖子和代码以包含此内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多