Detect if keys are pressed (and count) (not keylogging) in Python - python

I was wondering if there's a way to detect onkeyboardevents, but just to count them. I don't want to know exactly what the user has typed, but more like if he typed anything.
To be more specific:
If user presses key "a" on keyboard, I am interested to know that he pressed it (and at what time), not what the letter is.
Scenario:
User types in "hello stackoverflow" in the span of 2.5 seconds.
I want to know that he was typing something, don't care about what he typed.
Why do I want to do this? Some programs detect "keyloggers" as viruses (and they should) and I don't want my users to think it's something fishy.
Cheers.

in python keylogger itself, just keep a count in the place of recording the key stroke,
while(event.Ascii):
c++
refer: https://github.com/VigneshPrasadV

Related

Escape room (is there an alternative for if statements so you can print something based on whether a specific action was taken beforehand?) python

I’m sure my question is somewhat easy but I am new to programming and would love some help :).
The project is to create an escape room with three objects and depending on what was done prior, the player should be able to perform different tasks.
For example, in my escape room I have a pumpkin, a sign and box.
When the sign lights up, it shows a coin shaped circle on the box. Inside the pumpkin there’s a coin, that should be places on top of the box so it can open. The problem is, if the user first chooses to look at the box, they will see nothing. Only when the user chooses to look at the sigg, to turn on the sign and then to examine the box they will be able to see the circle. Moreover, they will only be able to place the coin on top if they had already smashed the pumpkin.
So I could make my code a lot of if statements (basically like:
if pumpkin_action == ‘smash’:
if box_action == ‘put coin on box’:
print(“You placed the coin on the box and BAM, the box opened revealing the key.”)
Elif pumpkin_action != ‘smash’:
if box_action == ‘examine box’: #since user wont even know the coin exists yet
print(“Weird coin-shaped circle on it. Wonder what that does…”)
But there are sooo many different possibilities of choices that the user can choose like if sign isn’t turned on they cant see anything on the box etc. So i was wondering if there’s something I can do that would make the code more compact than never ending if statements.
Thank you!

How to fix Python stopping when it shouldnt?

I have a program that detects when a certain key is pressed, and it will press another key. I'm not the greatest at python, but I've tried everything I could to get this to work. My game will end after the hello world statement I've added as a test. Could somebody please tell me a way to fix this? (Note: I want the code to always stay running, waiting for user input)
https://i.stack.imgur.com/QeJ3r.png

Block keyboard input in python

In Python, is there a way to prevent the user from entering anything via keyboard, so that terminal output does not get cluttered by unneeded input?
In my case, the user is supposed to press and hold a key to register a specific keyboard - so, the actual input (as in input()) is not needed, but only clutters the output and menu.
I would recommend pygame. http://www.pygame.org/download.shtml It allows you to read all keyboard keys as you like. Here's an example of how the code would look: How to get keyboard input in pygame?
It sounds like you want the terminal contents to redraw themselves on user input?
https://docs.python.org/3/howto/curses.html or https://docs.python.org/2.7/howto/curses.html depending on python version.
See also this question Dynamic terminal printing with python

Python ncurses - how to trigger actions while user is typing?

I am reading user input text with getstr(). Instead of waiting for the user to press enter, I would like to read the input each time it is changed and re-render other parts of the screen based on the input.
Is this possible with getstr()? How? If not, what's the simplest/easiest alternative?
Not with getstr(), but it's certainly possible with curses. You just have to read each keypress one at a time, via getch() -- and, if you want an editable buffer, you have to recreate something like the functionality of getstr() yourself. (I'd post an example, but what I have is in C rather than Python.)

Pygame: Graphical Input + Text Input

In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?
To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?
I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?
Thanks in advance.
You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).
The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.
I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"

Categories