Python Forum
Advance program when certain keys are pressed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advance program when certain keys are pressed?
#1
I'm trying to build a program that will quiz me on vim commands.
As of now, the program works, but I have to press enter after entering the command. Such as:

move down (I press "j" then "enter")
move to top of screen (I press "e" then "enter")
move forward to end of word (I press "e" then "enter")

Is there a way to advance the program without having to hit enter?


I have the questions and answers in a csv file
Here's the program I have:

#! /usr/bin/python3

import csv
from functions import clearScreen


clearScreen() # clearing the screen
studySet = input("Enter the set you would like to study: ")
with open(studySet) as f:     # opening the csv file that contains the questions/answers
	fileContents = csv.reader(f)
	flashcards = {rows[0]:rows[1] for rows in fileContents} # storing the contents of the csv file into a dictionary
questions = []
answers = []
for eachKey in flashcards.keys(): # this and next line storing the keys of dict in list called questions
	questions.append(eachKey)
	answers.append(flashcards[eachKey])
clearScreen()
counter = 0
while counter < len(questions):
	yourAnswer = input(questions[counter] + "\n")
	if yourAnswer == answers[counter]:
		clearScreen()
	elif yourAnswer != answers[counter]:
		clearScreen()
	    print("Sorry. That was not correct.")
		print("The correct answer is " + answers[counter])
		questions.append(questions[counter])
		answers.append(answers[counter])
	counter += 1

clearScreen()
Reply
#2
Look into the import keyboard
Reply
#3
Quote:Is there a way to advance the program without having to hit enter?
You will have to use a third party (does come with Python) module. One module is termios
import termios, sys, os
TERMIOS = termios
def getkey():
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
    new[6][TERMIOS.VMIN] = 1
    new[6][TERMIOS.VTIME] = 0
    termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
    c = None
    try:
        c = os.read(fd, 1)
    finally:
        termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
    return c
if __name__ == '__main__':
    print("type something...'q' to quit")
    s = ''
    while 1:
        c = getkey()
        if c == 'q':
            break
        print("captured key", c, ord(c))
        s = s + str(c)

    print(s) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code won't advance to next statement MCL169 2 722 Apr-11-2023, 09:44 PM
Last Post: Larz60+
  Getting button pressed number Moris526 4 2,374 Dec-14-2020, 01:41 AM
Last Post: Moris526
  How many times was the button pressed in pyglet rama27 0 1,889 Oct-10-2020, 10:26 AM
Last Post: rama27
  run different functions each time the same button is pressed? User3000 6 3,247 Jul-31-2020, 11:11 PM
Last Post: User3000
  Terminate a process when hotkey is pressed 66Gramms 0 2,210 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  Count to movement according to the time pressed button noartist 1 2,478 Feb-27-2019, 01:33 PM
Last Post: noartist
  What key pressed? ian 2 4,917 Jul-29-2018, 02:30 AM
Last Post: snippsat
  Advance properties read from xml files python1234 0 2,395 Apr-25-2018, 01:42 PM
Last Post: python1234
  Can't edit code after I've pressed enter. xBlackHeartx 2 11,979 Sep-02-2017, 10:04 PM
Last Post: nilamo
  I need help understanding how to use and run this program! Thanks in advance! tc1chosen 6 4,730 Sep-01-2017, 01:56 PM
Last Post: tc1chosen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020