[英]Priority when 2 keys are pressed at the same time - script for a game
Basically, I want it so that when I have 2 keys pressed together (both A and D, specifically), the last key pressed should have priority, and the key before that should be "suppressed" (no input).基本上,我想要这样,当我同时按下 2 个键(特别是 A 和 D)时,最后按下的键应该具有优先权,而之前的键应该被“抑制”(无输入)。
For example: in a game, when you press A, your character moves to the left, and when you press D, he moves to the right.例如:在游戏中,当你按下 A 时,你的角色向左移动,当你按下 D 时,他向右移动。 Pressing those 2 keys together makes the character stop.同时按下这两个键会使字符停止。 Now the thing here is that I don't want the character to stop, I want him to continue to move, based on the last key I pressed, even though I'm holding 2 keys at the same time.现在的问题是我不想让角色停下来,我想让他根据我按下的最后一个键继续移动,即使我同时按住 2 个键。
I thought this was going to be a trivial task but I actually got a little overwhelmed after trying to learn how to implement this (I'm a noob, sorry :C ), so I came here looking for some help on how to do this on AHK or any easy to compile scripting language that directly changes the input of a key.我认为这将是一项微不足道的任务,但在尝试学习如何实现这一点后,我实际上有点不知所措(我是菜鸟,抱歉:C),所以我来到这里寻求有关如何执行此操作的帮助在 AHK 或任何易于编译的脚本语言上直接更改键的输入。 I'm not trying to modify or create a game, so a script that remaps those keys is enough!我不是要修改或创建游戏,因此重新映射这些键的脚本就足够了!
Autohotkey example, following your " A and D, specifically " question: Autohotkey 示例,遵循您的“ A 和 D,特别是”问题:
for k,v in StrSplit("ad")
Hotkey, % "~$" v,Silveirous
Silveirous:
t:=SubStr(A_PriorHotkey,3)
if GetKeyState(t) and (A_ThisHotkey!=A_PriorHotkey)
Send {%t% up}
return
Documentation:文档:
for k,v in
, StrSplit()
, Hotkey,,,
, "~$"
,SubStr()
, A_Prior/ThisHotkey
, if
, GetKeyState()
, t:=... and ... !=
, Send {%t% up}
for k,v in
, StrSplit()
, Hotkey,,,
, "~$"
,SubStr()
, A_Prior/ThisHotkey
, if
, GetKeyState()
, t:=... and ... !=
, Send {%t% up}
Edit:编辑:
Another solution, made to be working as explained in OP's edit/comment:另一种解决方案,按照 OP 的编辑/评论中的解释进行工作:
#If GetKeyState("d","p")
~a::Send {d up}
~a up::Send {d down}
#If GetKeyState("a","p")
~d::Send {a up}
~d up::Send {a down}
#If
Make sure to mind the difference between #If
and if (expression)
usage cases.请务必注意#If
和if (expression)
用例之间的区别。
I'm too late but this could help someone in future :) Basically you need a variable to keep track of which direction the character faced first and act according to that.我为时已晚,但这可能会对将来的某人有所帮助:) 基本上,您需要一个变量来跟踪角色首先面对的方向并据此采取行动。 A code example could be:代码示例可能是:
let currentDir = 0;
let maxSpeed = (deltaTime * someConstant);
if (IsKeyDown(KEY_RIGHT) && IsKeyDown(KEY_LEFT))
{
if (currentDir == 1)
speed.x = -maxSpeed;
else if (currentDir == -1)
speed.x = maxSpeed;
}
else if (IsKeyDown(KEY_LEFT))
{
currentDir = -1;
speed.x = -maxSpeed;
}
else if (IsKeyDown(KEY_RIGHT))
{
currentDir = 1;
speed.x = maxSpeed;
}
else
{
speed.x = 0.0f;
currentDir = 0;
}
//And at last player's position would change every frame
player.x += speed.x;
Then when you press left while pressing right the character moves left without stopping and vice versa :)然后当你按下左同时按下右时,字符向左移动而不停止,反之亦然:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.