Welcome!

The kernel problem with recent updates has been solved. Find the solution here

Important information
-- Required MX 15/16 Repository Changes
-- Information on torrent hosting changes
-- Information on MX15/16 GPG Keys
-- Spectre and Meltdown vulnerabilities

News
-- Introducing our new Website
-- MX Linux on social media: here

Current releases
-- MX-18.3 Point Release release info here
-- Migration Information to MX-18 here
-- antiX-17.4.1 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

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

Post Reply
User avatar
Eric
Forum Novice
Forum  Novice
Posts: 49
Joined: Tue Jan 15, 2019 7:48 am

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

#1

Post by Eric » Wed Jan 30, 2019 9:32 am

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: 10138
Joined: Wed Jul 12, 2006 1:42 am

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

#2

Post by Adrian » 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] [/code] tags

bffarmer
Forum Novice
Forum  Novice
Posts: 13
Joined: Mon Jan 07, 2019 11:02 am

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

#3

Post by bffarmer » Wed Jan 30, 2019 10:53 am

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
Developer
Posts: 452
Joined: Wed Apr 16, 2014 10:31 pm

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

#4

Post by Old Giza » Wed Jan 30, 2019 1:37 pm

Does any of this help?

Stack Overflow
AutoPy, specifically: keyboard control

User avatar
Eric
Forum Novice
Forum  Novice
Posts: 49
Joined: Tue Jan 15, 2019 7:48 am

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

#5

Post by Eric » Wed Jan 30, 2019 1:44 pm

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
Forum Novice
Forum  Novice
Posts: 49
Joined: Tue Jan 15, 2019 7:48 am

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

#6

Post by Eric » Wed Jan 30, 2019 1:48 pm

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
Forum Novice
Forum  Novice
Posts: 49
Joined: Tue Jan 15, 2019 7:48 am

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

#7

Post by Eric » Wed Jan 30, 2019 11:43 pm

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
Forum Novice
Forum  Novice
Posts: 49
Joined: Tue Jan 15, 2019 7:48 am

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

#8

Post by Eric » Thu Jan 31, 2019 11:12 pm

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.

KoO
Forum Regular
Forum Regular
Posts: 557
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 » Fri Feb 01, 2019 1:27 am

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 : MX18.3 Debian10 (i3) , Liquorix , Intel Xeon E3-1241 v3 , 16gb , GTX970 . :linuxlove:
Lenovo T430 : Debian10 antiX17 (i3) , 4.20.12 , i5 , 12gb .
Lenovo X220 : Debian10 antiX17 (i3) , 4.19.170 , i5 , 8gb . BiosMod 1.46

ctt
Forum Novice
Forum  Novice
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 » Sat May 11, 2019 12:21 am

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”