3

I am writing a code that takes a user inputted word or phrase, and then returns a string of the 50 words before and after the given word. I want it to be able to scroll through the instances where that word or phrase is found by pressing the space bar to continue, or any other button to quit. The problem that I'm running into is that if I use the keyboard.wait() method I can only allow for one type of user input, but if I use any of the other keyboard methods that I have tried it doesn't stop my while loop and accept input. Here is the code I'm currently working with, any ideas would be helpful.

while True:
    keyboard.wait(" "):
    x = (x+1)%len(where_found)
    context = contents[where_found[x]-50:where_found[x]+50]
    context = " ".join(context)
    print(context+"\n")

where_found is a list containing all the index positions of the word that was searched for, or the first word if it was a phrase. content is a list created with the read().split() method of file reading. x is assigned a value of 1 before this section of code and allows the code to cycle through the instances where it was found.

Edit: I have tried both readchar.readkey() and msvcrt.getch() with no success. When they are reached my shell responds as if it is waiting for an input, but never actually accepts one. I am using windows and python 3.8.5.

1 Answers1

1

I tried to use keyboard.readkey from https://github.com/boppreh/keyboard but it kept returning two lines on each keypress.

Following a comment in this answer, here is a solution with https://github.com/magmax/python-readchar It's API is a lot simpler and (on windows) it does not seem to have problems with blocking.

(contents.txt has the text from this question)

import readchar

with open('contents.txt', 'r') as fo:
    contents = fo.read().split()

word = input('Please write a word to be searched: ')

print('\n### Press space for next match, any other key to exit\n')
print('matches found:')
x = -1
delta = 5
while True:
    try:
        x = contents.index(word, x+1)
        context = ' '.join(contents[max(x-delta, 0):x+delta+1])
        print(context)
        if readchar.readkey() != ' ':
            print('\n\nShutting down...')
            break
    except:
        print('\n\nthere are no more matches')
        break

Testing in terminal. It waits for a keypress, I pressed only spaces.

Please write a word to be searched: and

### Press space for next match, any other key to exit

matches found:
user inputted word or phrase, and then returns a string of
of the 50 words before and after the given word. I
doesn't stop my while loop and accept input. Here is the
before this section of code and allows the code to cycle


there are no more matches

Testing with other keys

Please write a word to be searched: and

### Press space for next match, any other key to exit

matches found:
user inputted word or phrase, and then returns a string of
of the 50 words before and after the given word. I


You pressed '\r'
Shutting down...
Please write a word to be searched: and

### Press space for next match, any other key to exit

matches found:
user inputted word or phrase, and then returns a string of


You pressed 'd'
Shutting down...
RichieV
  • 5,103
  • 2
  • 11
  • 24
  • you can change `except: break` to `except: x=-1` if you want to cycle until user presses any key other that space – RichieV Sep 27 '20 at 22:15
  • I can't seem to get this solution to work. Once my code hits the if readchar.readkey() != " ": command it pauses as if it were waiting for input, but nothing I press causes it to respond. I copied your code directly to see if I had just done it wrong, and had the same problem. This seems like it may have more to do with something on my end than yours , but I'm still at a loss. – Lane Burkholder Sep 27 '20 at 23:56
  • I'm using windows and python 3.8.5. I have also tried using msvcrt.getch() with the same poor results. The only other thing I can think of is to try doing it with pygame, but I wanted to find a different method. – Lane Burkholder Sep 28 '20 at 00:25
  • https://stackoverflow.com/q/24072790/6692898 there's `keyboard.is_pressed`, `pynput`, `PDCurses`, and [this other post](https://stackoverflow.com/a/31736883/6692898) with `ctypes` – RichieV Sep 28 '20 at 01:14
  • You should update your question with what you have tried so far so others can see it, hopefully it will get more attention – RichieV Sep 28 '20 at 01:17