[partially solved] I need a python script to hold a key down

Post Reply
Message
Author
User avatar
Eric
Posts: 30
Joined: Tue Jan 15, 2019 7:48 am

[partially solved] I need a python script to hold a key down

#1 Post by Eric »

Hi,
So I am trying to game in MX Linux, so far so good, BUT, there is no AutoHotKey in Linux, and I used to use it to hold down the move key in games to save wear and tear on my fingers.
The AutoHotKey script I had pushed and held the move key down when I pressed the activating key, and when I pressed the activating key a second time, it released the move key.

I downloaded AutoKey, which uses python, and is made to trigger python scripts with a key stroke, but I am too new at python to figure out what the script should be.

Any help would be appreciated.
Last edited by Eric on Thu Jan 31, 2019 11:15 pm, edited 1 time in total.

User avatar
Adrian
Developer
Posts: 8248
Joined: Wed Jul 12, 2006 1:42 am

Re: I need a python script to hold a key down

#2 Post by Adrian »

Can you link to AutoKey you downloaded or post here the relevant part of the script? You can post it in between [code] [/code] tags

bffarmer
Posts: 12
Joined: Mon Jan 07, 2019 11:02 am

Re: I need a python script to hold a key down

#3 Post by bffarmer »

I don't use linux for gaming but I am in the same boat using autohotkey to hold down the "w" key. I look forward to the answers given as I hope someday to be able to switch completely to linux.

User avatar
Old Giza
Posts: 426
Joined: Wed Apr 16, 2014 10:31 pm

Re: I need a python script to hold a key down

#4 Post by Old Giza »

Does any of this help?

Stack Overflow
AutoPy, specifically: keyboard control

User avatar
Eric
Posts: 30
Joined: Tue Jan 15, 2019 7:48 am

Re: I need a python script to hold a key down

#5 Post by Eric »

Adrian wrote: Wed Jan 30, 2019 10:30 am Can you link to AutoKey you downloaded or post here the relevant part of the script? You can post it in between

Code: Select all

 
tags
Autokey was a program I installed via Synaptic Package Manager.
It was listed on some websites as a Linux alternative to AutoHotKey.

It uses Python instead of AutoHotKeys own scripting language.
I don't know Python at all, so there is no script to post.

Autokey's interface is pretty straight forward, there is a place to indicate what key will trigger the script, and a place to type in the script.
I need to know what syntax to type in, to get the "push and hold, toggle" behavior that I got in Autohotkey.

User avatar
Eric
Posts: 30
Joined: Tue Jan 15, 2019 7:48 am

Re: I need a python script to hold a key down

#6 Post by Eric »

Old Giza wrote: Wed Jan 30, 2019 1:37 pm Does any of this help?

Stack Overflow
AutoPy, specifically: keyboard control
Yes, this is a good start.
I am still struggling with exact syntax though.

User avatar
Eric
Posts: 30
Joined: Tue Jan 15, 2019 7:48 am

Re: I need a python script to hold a key down

#7 Post by Eric »

Ok
I installed AutoPy with apt-get

BUT
The program AutoKey will not recognize the module.

When i use the command
import autopy

I get the following error message:
Script name: 'New Script'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/autokey/service.py", line 454, in execute
exec script.code in scope
File "<string>", line 2, in <module>
ImportError: No module named autopy

I tried looking in the service.py file.
Its hundreds and hundreds of lines of code, no way am I messing with it, unless I know exactly what i am doing.

Any ideas?

User avatar
Eric
Posts: 30
Joined: Tue Jan 15, 2019 7:48 am

Re: I need a python script to hold a key down

#8 Post by Eric »

Ok, I have a partial work around, it involves one key to start running, another to stop.

I am writing all this, because at least one other person is interested in autorun in none autorun games.

First, I had a huge trouble getting Autopy to work.
Turns out it was because I installed it with apt-get
To work, it must be installed with pip, which you must first install with apt-get

Then after MUCH messing around, I gave up on Autopy, as the command autopy.key.toggle had syntax I could just not get to work.

So, I tried using pyautogui, also installed via pip

Code: Select all

pip install pyautogui
This had very straight forward syntax

Code: Select all

pyautogui.keyDown('num2')
and

Code: Select all

pyautogui.keyUp('num2')
But, getting the toggling logic to work right was tricky, and I had to use a text file to hold one word, either False or True, which the script alternated writing one of those words to the file.

Finally got the toggling to work, but ran into a hardware problem.
When the keyDown was active, AutoKey would not register the keystroke to activate the script a second time, having one key down precluded it "seeing" the activating keystroke.

Perhaps your keyboard is different, so here is the code in case it will work for you

Code: Select all

import pyautogui
ar = open("autorun.txt", "r+") 
down = ar.read().strip()
ar.close()
ar = open("autorun.txt","w")
if (down == "True"): 
    ar.write("False")
    pyautogui.keyDown('insert')
else: 
    ar.write("True")
    pyautogui.keyUp('insert')
ar.close()
Before using this code, make a text file in your home dir, called autorun.txt and put the word False in it.

Anyway, would not work for me, as the keystroke to trigger the script would not register when the keyDown was active.

So, I made a cheap workaround. One key to activate autorun (doing a keyDown call), then just hitting the movement key by hand, which seemed to cancel the keyDown.
If you have the activation key right next to the movement key, its not that bad.
I have keypad1 to activate, and keypad2 as the movement key.

Code: Select all

import pyautogui
pyautogui.keyUp('num2')
pyautogui.keyDown('num2')
Here is a picture of what it looks like in AutoKey
Autorun2.png
I hit keypad1 to start running, keypad2 to stop.
Not so bad.
You do not have the required permissions to view the files attached to this post.

User avatar
KoO
Posts: 491
Joined: Fri Feb 10, 2017 1:21 am

Re: [partially solved] I need a python script to hold a key down

#9 Post by KoO »

I also was thinking of a bit of gaming in linux. Have a heap of steam games all FPS don't even know if steam games for windows will work with linux steam. Doe's linux steam have like wine build in or something. So you have to keep pressing the (w) key up and down to more forward. a s d same.
I have installed some linux drivers in the past for my Razer keyboards and mice. I don't know they would with gaming. If you own Logitech stuff...


Back to the real topic: Not sure if any this will help ?

https://nitratine.net/blog/post/how-to- ... in-python/ = How to Make Hotkeys in Python

https://appdb.winehq.org/objectManager. ... &iId=17738 = Linux port of AutoHotkey via wine. Uses AutoHotkey.dll, xdotool .
Main : MX 19.1-AHS (i3) 5.4.13-1~mx19+1, Asus B450-i AMD 5 3600 , 32gb Hyper-X 3200 , GTX970 . :linuxlove:
Lenovo T430 : Debian10 antiX17 (i3) , 4.20.12 , i5 , 12gb .
Lenovo X220 : Test Machine (ATM)

ctt
Posts: 9
Joined: Sun Jan 20, 2019 10:27 am

Re: [partially solved] I need a python script to hold a key down

#10 Post by ctt »

https://faq.i3wm.org/question/6901/mous ... ard.1.html
xbindkeys ...
'xdotool keydown --clearmodifiers w '
Alt + w
'xdotool keyup --clearmodifiers w '
Alt + w + Release
or
$ xdotool keydown w;sleep 3;xdotool keyup w
https://askubuntu.com/questions/122161/ ... d-xkeybind

i have not tested the xbindkey methods. Or maybe buy a cheap chinese joystick?

Post Reply

Return to “General”