Asori12 Asori12 - 3 years ago 71
Python Question

How do I count everytime I press a key down?

I want to count everytime I hit the left button my code looks like this

import pyautogui
import pygame
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print (sequentially the amount of times i've hit left)

Answer Source

Just store the data in a variable and increment it:

import pyautogui
import pygame
Number_of_Presses = 0
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
       if event.key == pygame.K_LEFT:
            Number_of_Presses += 1
            print (Number_of_Presses)

Should work great.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download