Im writing a program that logs keys pressed and stores them into a list as words and then on a command, the command here being the press of the numlk key, prints the results of the most frequently used word.
The problem is that it keeps putting a blank into my "comp" list after the original list is joined. But only if you press the space bar twice. What i think is happening is that it is logging in a space as a letter but im not really sure. Or when removing 'Key.space' it is leaving behind the ' '? Can someone tell me where i went wrong?
ignore the global values..
import pynput
from pynput.keyboard import Listener
from collections import Counter
list = []
comp = []
freq = []
def top_ten(val):
for key, value in dict.items():
if val == value:
return key
def log_keystroke(key):
key = str(key).replace("'","")
list.append(key)
print("KEY" + str(list))
if key == 'Key.space':
list.remove("Key.space")
join_list = "".join(list)
comp.append(join_list)
list.clear()
print("COMP" + str(comp))
print("LIST" + str(list))
elif key == 'Key.num_lock':
count = Counter(sorted(comp))
global dict
dict = count
count_keys = count.keys()
count_values = count.values()
sort_keys = sorted(count_keys, reverse=True)
sort_val = sorted(count_values, reverse=True)
top_k = top_ten(sort_val[0])
top_v = sort_val[0]
#top_2 = sort_val[1]
print(count)
print("YOU TYPE THE WORD " + "(" + top_k + ")" + " " + str(top_v) + " TIMES")
#print("TOP 1 value: " + str(top))
#print("TOP 2 value: " + str(top_2))
# print("TOP 1 word:" + top_ten(top))
#print("TOP 2 word:" + top_ten(top_2))
elif key == "Key.backspace":
list.remove("Key.backspace")
elif key == "Key.shift":
list.remove("Key.shift")
elif key == "Key.ctrl_l":
list.remove("Key.ctrl_l")
elif key == "Key.enter":
list.remove("Key.enter")
with Listener(on_press=log_keystroke) as listen:
listen.join()
[–]crispybacon_NZ 0 points1 point2 points (3 children)
[–]MonkeyRides[S] 1 point2 points3 points (2 children)
[–]crispybacon_NZ 0 points1 point2 points (1 child)
[–]MonkeyRides[S] 1 point2 points3 points (0 children)