Welcome to the list and to AutoKey!
I know very little Python, so some of my ideas may be wrong. I also know
nothing about Minecraft.
Here goes anyway:
Rather than just trying to solve your problem, I'm going to "critique"
your code and mention a number of general issues that may be helpful to
consider
in any code you write.
TL;DR: Just skip to 7).
1) I think
while True:
would give you a cleaner test.
2) I believe that Python's block structure syntax demands that the code
beneath (within) the while be indented.
The way you have it *might* be an infinite empty loop.
3) With an infinite loop, you still need a way to terminate it (better
than just quitting AutoKey) - like typing another key and having it
break out of the loop.
4) What you have appears to be a very tight loop - which means it will
use a lot of resources spinning its wheels.
Whenever you do something like this, you should introduce some sort of
pause or delay like
import time
...
time.sleep(1.0) # to sleep 1 second
5) You probably shouldn't use a plain character to activate a macro -
especially one that already has a special meaning in the application
context. This is just asking for trouble (and, in your case, might even
cause some bizarre recursion to take place). That's why all the modifier
keys are available. Choose a key combination that doesn't do anything in
the native application (or in any other layers of software that may be
in play.) E.g. Assigning a macro to something like ALT+F4 would most
probably end up closing your application window when you press Alt+F4
because the window manager would act on it before AutoKey or your
application saw it.
6) Since all you want to do is toggle the state of whether the computer
thinks you're holding down the V key, you can use a saved variable that
is essentially Boolean.
It can have any two values. It doesn't matter what they are as long as
your code can tell them apart. True and False would be good values. They
make your logic very simple. (And I do believe saved variables can be
anything.)
> There are (at least) two ways to deal with persistent variables.
>
> 1) Within one macro:
> AutoKey has a way to store variables which are "local" to a specific
> macro, but are retained from invocation to invocation so you can
> preserve state information.
>
> If you go to
>
https://code.google.com/p/autokey/wiki/SampleScripts
>
> and scroll down to Persistent Value, it shows how to use a persistent
> variable.
>
> 2) Within whatever scope you require:
> Since AutoKey macros are written in Python, you can take advantage of
> that to do almost anything you can think of. In this case, you could
> save variable information in environment variables or in regular files
> and read and write them from within your macros.
>
> This is pretty simple as long as you make sure you have only one thing
> reading and writing these at one time so the data you put there is
> what you get back when you read it and not something that got put
> there subsequently by another process.
7) So, after all that, I think all your macro has to do is see if the V
key is already being pressed (by examining the saved variable for the
value of True - after having initialized it as False). If it has been
pressed, then release it, set the saved variable to False and exit. If
it hasn't been pressed, (saved value is False), then press it, set the
saved value to True and exit.
But since holding down keys is a special kind of situation, I have no
idea if that would persist between macro calls.
Joe
On 02/11/2015 08:38 AM,
felix...@gmail.com wrote:
> Hi,
>
> I would like to toggle a key with an on/off mode. It would be actually
> used in Minecraft for the sneak mode, instead of pressing the key, I
> would just have to click one time to sneak, click one time to remove.
>
> I am kinda new to Python and AutoKey (just discovered it yesterday),
> and the script I have done is not working. I added some comments to
> tell you what I think my script is doing.
>
> |
> x =1 # infinite loop, to make the script
> work all
> whilex >0: # the time I play Minecraft.
> ifstore.has_key("v"): # if the user press "v"
> key =store.has_key("random") # We change the stored key to make
> the programme work
> whilekey !="v": # while the key pressed is different
> from "v", we basically hold down
> keyboard.press_key(v) # the "v" key.
> ifstore.has_key("v"): # If the user press again "v", we
> stop holding down "v" key.
> keyboard.release_key(v)
> |
> ||||
> I don't think the |store.set_value(key, value) and
> ||store.get_value(key)||fonction|s would be useful in my case, because
> if I understood well the API and the scripts examples, it stores a
> number and not a key. Furthermore, I use the "v" key also to chat in
> Minecraft (multiplayer), and I don't think it would be possible to do
> a counter. It would maybe be possible if I "desactivate" the counter
> and the script every time I press "t" (the key to chat in Minecraft),
> but it seems for me far away from my competance... What do you think?
>
> I also set an hotkey (ctrl+e) to start the script.
>
> I will keep working on it and tell you if I succeed.
>
>
> Regards
>
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
autokey-user...@googlegroups.com
> <mailto:
autokey-user...@googlegroups.com>.
> For more options, visit
https://groups.google.com/d/optout.