Search on blog:

How to get keycode in Python using Tkinter or PyGame

Tkitner

This code uses tkinter to get pressed/released key and display its keycode (scancode), keysym, char (unicode)

import tkinter as tk

# --- functions ---

def on_press(event):
    # display pressed key
    #print(event)
    print('PRESS   | keycode: {}, keysym: {}, char: {}'.format(event.keycode, event.keysym, event.char))

def on_release(event):
    # display released key
    #print(event)
    print('RELEASE | keycode: {}, keysym: {}, char: {}'.format(event.keycode, event.keysym, event.char))

    # exit program on ESC
    #if event.keysym == 'Escape':
    #     root.destroy()

# --- main ---

root = tk.Tk()

# assign functions to events
root.bind('<KeyPress>', on_press)
root.bind('<KeyRelease>', on_release)

root.mainloop()

Result

PRESS   | keycode: 38, keysym: a, char: a
RELEASE | keycode: 38, keysym: a, char: 
PRESS   | keycode: 54, keysym: c, char: c
RELEASE | keycode: 54, keysym: c, char: 
PRESS   | keycode: 12, keysym: 3, char: 3
RELEASE | keycode: 12, keysym: 3, char: 
PRESS   | keycode: 50, keysym: Shift_L, char: 
PRESS   | keycode: 13, keysym: dollar, char: $
RELEASE | keycode: 13, keysym: dollar, char: 
RELEASE | keycode: 50, keysym: Shift_L, char: 
PRESS   | keycode: 50, keysym: Shift_L, char: 
PRESS   | keycode: 10, keysym: exclam, char: !
RELEASE | keycode: 10, keysym: exclam, char: 
RELEASE | keycode: 50, keysym: Shift_L, char: 

PyGame

This code uses pygame to get pressed/released key and display its scancode (keycode), key, unicode (char)

import pygame

# init pygame and create window which will be get events from system
pygame.init()
screen = pygame.display.set_mode((800,600))

running = True
while running:

    for event in pygame.event.get():
        # close window with button [X]
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYDOWN:
            # display pressed key
            #print(event)
            print('PRESS   | scancode: {}, key: {}, unicode: {}'.format(event.scancode, event.key, event.unicode))

        elif event.type == pygame.KEYUP:
            # display released key
            #print(event)
            print('RELEASE | scancode: {}, key: {}'.format(event.scancode, event.key)) # `KEYUP` doesn't have `event.unicode`

            # exit on ESC 
            #if event.key == pygame.K_ESCAPE:
            #   running = False

# exit
pygame.quit()

Result

PRESS   | scancode: 38, key: 97, unicode: a
RELEASE | scancode: 38, key: 97
PRESS   | scancode: 54, key: 99, unicode: c
RELEASE | scancode: 54, key: 99
PRESS   | scancode: 50, key: 304, unicode: 
PRESS   | scancode: 13, key: 52, unicode: $
RELEASE | scancode: 13, key: 52
RELEASE | scancode: 50, key: 304
PRESS   | scancode: 50, key: 304, unicode: 
PRESS   | scancode: 10, key: 49, unicode: !
RELEASE | scancode: 10, key: 49
RELEASE | scancode: 50, key: 304

PyGame: Przeciąganie obiektu po ekranie za pomocą myszy

Najpierw trzeba stworzyć obiekt do przesuwania (jego wielkość i pozycja jest w Rect()) oraz zmienną do przechowywania informacji czy obiekt jest przeciągany

rectangle = pygame.rect.Rect(176, 134, 30, 30)
rectangle_draging = False

Następnie należy użyć:

MOUSEBUTTONDOWN aby sprawdzić czy obiekt został kliknięty i ustawić drag = True i zapamiętać offset pomiędzy …

PyGame draw background checkerboard [GB]

python pygame - draw background checkerboard
import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25

GRAY_1 = (128,128,128)
GRAY_2 = (192,192,192)

# --- main ---

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# ---

background = pygame.surface.Surface(screen.get_size())

color = GRAY_1
for x in range(0, SCREEN_WIDTH, 40):
    for y in range(0, SCREEN_HEIGHT, 40):
        pygame …

Pygame: zdarzenia KEYDOWN, KEYUP i funkcje get_pressed(), get_mods().

python pygame zdarzenia i funkcje dotyczące klawiszy

Zdarzenia KEYDOWN, KEYUP to pojedyńcze sygnały, które pojawiają się na początku i na końcu trzymania wciśniętego klawisza.

Funkcja pygame.key.get_pressed() dostarcza przez cały czas informację, że dany klawisz jest trzymany wciśnięty. Wykorzystuje do tego tablicę z wartościami True/False. Ta funkcja wymaga wywoływania (w pętli) funkcji pygame.event.get …

Najprostszy program w PyGame

# import potrzebnego modulu (i podmodulow)
import pygame

# inicjalizacja modulu (i podmodulow)
pygame.init()

# stworzenie okna o powierzchni do rysowania 800x600
screen = pygame.display.set_mode((800, 600))

# uruchomienie glownej petli (mainloop) do obslugi zdarzen
running = True

while running:

    # pobieranie kolejnych zdarzen
    for event in pygame.event.get():

        # jesli kliknieto w przycisk …

Użycie klasy i timera w PyGame na przykładzie zegara

[![Zegary w Pygame](http://blog.furas.pl/images/2014/02/clock_with_class.png "Zegary w Pygame


")](http://blog.furas.pl/uzycie-klasy-i-timera-w-pygame-na-przykladzie-zegara/clock_with_class/) Część mojej odpowiedzi na pytanie "Clock in Python" zadane w dziale "Pygame" na StackOverflow.com: ----------- Umieszczenie zegara w klasie ---------------------------- ::::python #!/usr/bin/python from pygame import * from pygame.locals …

« Page: 1 / 1 »