问题
I want to create a program in autohotkey so that when ~pdo is typed it replaces it with a long line of code. How do I do it so that the symbols inside do not get formatted as autohotkey syntax?
I have tried the following code:
~pdo:Send,
$pdo = new PDO('mysql:dbname=chat;host=localhost', 'root', 'simon sleeping123!@#');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
date_default_timezone_set('America/New_York');
In return I get the error message: Line Text: $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); Error: Invalid hotkey.
回答1:
I'd recommend utilizing the clipboard and CTRL+V for such a long input. If not pasting the text, you'd need to send that in text mode to avoid translating certain characters to certain buttons. E.g #
to the Windows key.
Also, you'll need to add line breaks. More on that below.
Firstly, use a hotstring to trigger when ~pdo
is typed. Use whichever options you see fit. I'd assume you'll be fine with just the * option.
And to send multi line stuff, you can either explicitly specify line breaks with a line feed character `n
(docs):
$pdo = new PDO('mysql:dbname=chat;host=localhost', 'root', 'simon sleeping123!@#');`n`n$pdo->setAttribute(PDO::ATTR_EM...
Or you can more conveniently use a continuation section:
:*:~pdo::
Clipboard := "
(LTrim
$pdo = new PDO('mysql:dbname=chat;host=localhost', 'root', 'simon sleeping123!@#');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
date_default_timezone_set('America/New_York');
)"
SendInput, ^v
return
And there's your finished script. ^v
means CTRL+V and the LTrim
(docs) option is used do we can still properly format the code without adding extra spaces to the actual text in the continuation section.
回答2:
You are looking for a Hotstring rather than a Hotkey. A hotkey triggers when keys are held down together, whereas a hotstring triggers when keys are typed in sequence.
Furthermore, seeing how this is a multiline hotstring, take a look at this to let it work.
Based on this, here is my code (triggered when "~pdo" is typed):
:*:~pdo::
MyMultilineHotstring =
(
$pdo = new PDO('mysql:dbname=chat;host=localhost', 'root', 'simon sleeping123!@#');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
date_default_timezone_set('America/New_York');
)
SendInput, %MyMultilineHotstring%
来源:https://stackoverflow.com/questions/65676301/how-to-disable-syntax-characters-in-autohotkey