0

Hi im currently working on a triggerbot for Valorant (I don’t wanna cheat i just want to learn python coding and combine it with Pixel detection and input making) and want to ask how i disable the Keyboard input and enable it right after an other task. Something like this:

Import mouse
Import keyboard

//diable keyboard here
Mouse.click(„left“)
//enable keyboard here

This is just a cut of the Programm if the rest is required i can add this of course.

I tried finding something about it but it didn’t work for me. I would appreciate help. Sorry for my maybe bad English.

1

2 Answers 2

1

Using the keyboard module, we can block every key on the keyboard:

import keyboard

for key in range(150): #150 should be enough for all the keys on the keyboard
    keyboard.block_key(key) #block the key until unblocked

#do stuff

for key in range(150):
    keyboard.unblock_key(key) #unblocks all the keys

To make this a little easier and readable in your program, you could create two simple functions for it:

import keyboard

def block():
    for key in range(150):
        keyboard.block_key(key)

def unblock():
    for key in range(150):
        keyboard.unblock_key(key)

block()
#do stuff
unblock()
Sign up to request clarification or add additional context in comments.

Comments

0

You can suppress keyboard input using built-in (standard) Python modules.

It is common to also want to suppress signals while keyboard input is blocked. Therefore, this utility class facilitates that too (by default).

from termios import tcgetattr, tcsetattr, TCSAFLUSH
from sys import stdin
from tty import setraw
import signal


class Blocker:
    def __init__(self, ignore_signals=True):
        self._ignore_signals = ignore_signals

    @staticmethod
    def siglist():
        signames = [
            "SIGABRT",
            "SIGINT",
            "SIGTERM",
            "SIGBREAK",
            "SIGALRM",
            "SIGCONT",
            "SIGHUP",
            "SIGUSR1",
            "SIGUSR2",
            "SIGWINCH",
        ]
        _siglist = []
        for signame in signames:
            if (sig := getattr(signal, signame, None)) is not None:
                _siglist.append(sig)
        return _siglist

    def __enter__(self):
        self._fd = stdin.fileno()
        self._attr = tcgetattr(self._fd)
        setraw(self._fd)
        if self._ignore_signals:
            self._sigfuncs = {
                s: signal.signal(s, signal.SIG_IGN) for s in Blocker.siglist()
            }
        else:
            self._sigfuncs = None
        return self

    def __exit__(self, *_):
        if self._sigfuncs is not None:
            for kv in self._sigfuncs.items():
                signal.signal(*kv)
        if self._attr is not None:
            tcsetattr(self._fd, TCSAFLUSH, self._attr)


import time
# during sleep, keyboard input and observable signals will be ignored
with Blocker():
    time.sleep(5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.