Python Forum
pygame and shift branching?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame and shift branching?
#1
Sorry, I know this is another pygame problem, but I hope some kind person can help.

I can find examples on the google how to check for key presses such as a+Shift. I can't fond how to check for these separately, I want to do something like:

If SHIFT
if a pressed:
if b pressed:

if Not SHIFT
if a pressed:
if b pressed:

This must be possible?

Thanks.
Reply
#2
import pygame as pg

pg.init()

screen = pg.display.set_mode((800,600))
done = False

while not done:
    keys = pg.key.get_pressed()
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    if keys[pg.K_LSHIFT] and keys[pg.K_a]:
        print('left shift and a key pressed')
    elif keys[pg.K_LSHIFT] and keys[pg.K_b]:
        print('left shift and b key pressed')
    elif keys[pg.K_a]:
        print('only a key pressed')
    elif keys[pg.K_b]:
        print('only b key pressed')
    pg.display.update()
or

import pygame as pg

pg.init()

screen = pg.display.set_mode((800,600))
done = False

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_a and pg.key.get_mods() & pg.KMOD_SHIFT:
                print('shift and a keys pressed')
            elif event.key == pg.K_b and pg.key.get_mods() & pg.KMOD_SHIFT:
                print('shift and b keys pressed')
            elif event.key == pg.K_a:
                print('only a key pressed')
            elif event.key == pg.K_b:
                print('only b key pressed')
    pg.display.update()
Recommended Tutorials:
Reply
#3
Also with KEYUP and KEYDOWN. Mod is pass to the event.
import pygame

class Game:
    def __init__(self, caption, width, height):
        # Basic pygame setup
        pygame.display.set_caption(caption)
        self.rect = pygame.Rect(0, 0, width, height)
        self.surface = pygame.display.set_mode(self.rect.size)
        self.clock = pygame.time.Clock()

    def mainloop(self):
        self.running = True

        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.mod & pygame.KMOD_SHIFT:
                        # event.unicode handles shift itself
                        print('Shift', event.unicode)
                    else:
                        print('Key pressed', event.unicode)
                elif event.type == pygame.QUIT:
                    self.running = False

            self.surface.fill(pygame.Color('Darkblue'))

            pygame.display.update()
            self.clock.tick(30)

if __name__ == '__main__':
    pygame.init()
    game = Game('Example', 800, 600)
    game.mainloop()
    pygame.quit()
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Drawn line shift when that surface is copied to another in pygame Leo_Red 4 3,360 Feb-11-2021, 06:33 AM
Last Post: Leo_Red

Forum Jump:

User Panel Messages

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