antisycop
antisycop

Reputation: 93

Check Capslock In Pygame.Key

I wrote a notetaking app, which provides with easier usage, but I cannot check whether the caps lock button is on or off. I can get the value when it is turned on, but there is not any response when it is turned off. In other words, I can get the output of the key when the led is turned on, but not when it is turned off.

import pygame
import random
import time
import subprocess
from pygame.locals import *
time_a = time.time()
slides = []
POS = (0,0)
capslock = False
altcizgi = False
italic = False
bold = False
yazi = ""
character = ""
time_d = 0
sayim = 0
time_b = 0
buyukluk = 30
liste = []
r = ""
rty = ""
time_c = 0
durum = 0
sol_bas = -100
WHITE= (255,255,255)
BLACK = (0,0,0)
GREY = (200,200,200)
ORANGE = (200, 100, 50)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
TRANS = (1, 1, 1)
firca_ucu = 3
# initializations
pygame.init()
infoObject = pygame.display.Info()
screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
pygame.display.set_caption('Noting')
pygame.display.set_mode((infoObject.current_w, infoObject.current_h), pygame.FULLSCREEN)
background = pygame.Surface(screen.get_size())
background = background.convert()
color = (0, 0, 0)
startPos = (0, 0)
clock = pygame.time.Clock()
background.fill((255, 255, 255))
image = pygame.image.save(background,'image.png')
font = pygame.font.SysFont('arial', 150)
colorName = "Black"
welcomeFont = pygame.font.SysFont('arial', 50)
colorConfigFont = pygame.font.SysFont('arial', 35)
welcomeScreen = True
# mainloop
while 1:
    # welcomescreen
    while welcomeScreen:
        for i in pygame.event.get():
            mainScreenPressed = pygame.key.get_pressed()
            screen.fill((0,0,0))
            screen.blit(welcomeFont.render("Paint Program Keys", True, (0, 255, 0)), (100, 50))
            screen.blit(colorConfigFont.render("Left: Decrease Punto", True, (0, 255, 0)), (900, 150))
            screen.blit(colorConfigFont.render("Right: Increase Punto", True, (0, 255, 0)), (900, 200))
            screen.blit(colorConfigFont.render("Up: Increase Brush Size", True, (0, 255, 0)), (900, 250))
            screen.blit(colorConfigFont.render("Down: Decrease Brush Size", True, (0, 255, 0)), (900, 300))
            screen.blit(colorConfigFont.render("Space: Draw On/Off", True, (0, 255, 0)), (900, 350))
            screen.blit(colorConfigFont.render("Backspace: Backspace", True, (0, 255, 0)), (900, 400))
            screen.blit(colorConfigFont.render("Control: Italic", True, (0, 255, 0)), (900, 450))
            screen.blit(colorConfigFont.render("F7: ", True, (0, 255, 0)), (900, 500))
            screen.blit(colorConfigFont.render("Mouse-Click: Text", True, (0, 255, 0)), (900, 550))
            screen.blit(colorConfigFont.render("ESC: Main Menu", True, (0, 255, 0)), (100, 150))
            screen.blit(colorConfigFont.render("F1: Draw Square", True, (0, 255, 0)), (100, 200))
            screen.blit(colorConfigFont.render("F2: Draw Circle", True, (0, 255, 0)), (100, 250))
            screen.blit(colorConfigFont.render("F3: Draw Triangle", True, (0, 255, 0)), (100, 300))
            screen.blit(colorConfigFont.render("F4: Import", True, (0, 255, 0)), (100, 350))
            screen.blit(colorConfigFont.render("F5: Save", True, (0, 255, 0)), (100, 400))
            screen.blit(colorConfigFont.render("F6: Clear", True, (0, 255, 0)), (100, 450))
            screen.blit(colorConfigFont.render("F7: Previous Song", True, (0, 255, 0)), (100, 500))
            screen.blit(colorConfigFont.render("F8: Stop/Play Song", True, (0, 255, 0)), (100, 550))
            screen.blit(colorConfigFont.render("F9: Next Song", True, (0, 255, 0)), (500, 150))
            screen.blit(colorConfigFont.render("F10: Mute", True, (0, 255, 0)), (500, 200))
            screen.blit(colorConfigFont.render("F11: Decrease Volume", True, (0, 255, 0)), (500, 250))
            screen.blit(colorConfigFont.render("F12: Increase Volume", True, (0, 255, 0)), (500, 300))
            screen.blit(colorConfigFont.render("Tab: Undo", True, (0, 255, 0)), (500, 350))
            screen.blit(colorConfigFont.render("Alt: Underline", True, (0, 255, 0)), (500, 400))
            screen.blit(colorConfigFont.render("Enter: Caps-Lock", True, (0, 255, 0)), (500, 450))
            screen.blit(colorConfigFont.render("Shift: Draw On/Off", True, (0, 255, 0)), (500, 500))
            screen.blit(colorConfigFont.render("CMD: Bold", True, (0, 255, 0)), (500, 550))
            screen.blit(welcomeFont.render("Press 'ESC' to continue", True, (0, 255, 0)), (100, 700))
            pygame.display.flip()
            if mainScreenPressed[K_ESCAPE]:
                durum = -1
                welcomeScreen = False
                time_d = time.time()


            class Slider():
                def __init__(self, name, val, maxi, mini, pos):
                    self.val = val  # start value
                    self.maxi = maxi  # maximum at slider position right
                    self.mini = mini  # minimum at slider position left
                    self.xpos = 1300  # x-location on screen
                    self.ypos = pos
                    self.surf = pygame.surface.Surface((100, 50))
                    self.hit = False  # the hit attribute indicates slider movement due to mouse interaction

                    self.txt_surf = font.render(name, 1, BLACK)
                    self.txt_rect = self.txt_surf.get_rect(center=(50, 15))

                    # Static graphics - slider background #
                    self.surf.fill((100, 100, 100))
                    pygame.draw.rect(self.surf, GREY, [0, 0, 100, 50], 3)
                    pygame.draw.rect(self.surf, ORANGE, [10, 10, 80, 10], 0)
                    pygame.draw.rect(self.surf, WHITE, [10, 30, 80, 5], 0)

                    self.surf.blit(self.txt_surf, self.txt_rect)  # this surface never changes

                    # dynamic graphics - button surface #
                    self.button_surf = pygame.surface.Surface((20, 20))
                    self.button_surf.fill(TRANS)
                    self.button_surf.set_colorkey(TRANS)
                    pygame.draw.circle(self.button_surf, BLACK, (10, 10), 6, 0)
                    pygame.draw.circle(self.button_surf, ORANGE, (10, 10), 4, 0)

                def draw(self):
                    """ Combination of static and dynamic graphics in a copy of
                the basic slide surface
                """
                    # static
                    surf = self.surf.copy()

                    # dynamic
                    pos = (10 + int((self.val - self.mini) / (self.maxi - self.mini) * 80), 33)
                    self.button_rect = self.button_surf.get_rect(center=pos)
                    surf.blit(self.button_surf, self.button_rect)
                    self.button_rect.move_ip(self.xpos, self.ypos)  # move of button box to correct screen position

                    # screen
                    screen.blit(surf, (self.xpos, self.ypos))

                def move(self):
                    """
                The dynamic part; reacts to movement of the slider button.
                """
                    self.val = (pygame.mouse.get_pos()[0] - self.xpos - 10) / 80 * (self.maxi - self.mini) + self.mini
                    if self.val < self.mini:
                        self.val = self.mini
                    if self.val > self.maxi:
                        self.val = self.maxi

    # paint screen
    pressed = pygame.key.get_pressed()
    if pressed[K_RMETA] or pressed[K_LMETA] and pressed[K_q]:
        screen.fill(BLACK)
        rn = time.strftime("%H.%M %d:%m:%Y")
        pygame.image.save(background, "/Users/Esat/Desktop/Noting/Saved_Work_at_"+rn+".jpg")
        exit()
    if pressed[K_F1]:
        pass
    if pressed[K_F2]:
        pass
    if pressed[K_ESCAPE]:
        if time.time()-time_d>0.5:
            welcomeScreen = True
    if pressed[K_F4]:
        input_screen = True
        background = pygame.image.load("/Users/Esat/Desktop/Noting/Imports/"+data+".jpg")
    if pressed[K_F5]:
        if time.time()-time_c>5:
            rn = time.strftime("%H.%M %d:%m:%Y")
            pygame.image.save(background, "/Users/Esat/Desktop/Noting/"+rn+".jpg")
            background.fill(WHITE)
            screen.fill(WHITE)
            time_c = time.time()
        else:
            pass
    if pressed[K_F6]:
        background.blit(font.render(yazi, True, (color)), (POS))
        pygame.image.save(background, 'image.png')
        liste = []
        yazi = ""
        r = ""
        background.fill((255,255,255))
    if pressed[K_F7]:
        pass
    if pressed[K_F8]:
        pass
    if pressed[K_F9]:
        pass
    if pressed[K_F10]:
        pass
    if pressed[K_F11]:
        pass
    if pressed[K_F12]:
        pass
    if pressed[K_LMETA] or pressed[K_RMETA]:
        bold = True
    if pressed[K_LALT] or pressed[K_RALT]:
        altcizgi = True
    if pressed[K_LCTRL] or pressed[K_RCTRL]:
        italic = True
    if pressed[K_UP]:
        if rty == "eraser":
            firca_ucu = firca_ucu * 1.04
        elif rty != "eraser":
            firca_ucu = firca_ucu*1.02
    if pressed[K_DOWN]:
        firca_ucu = firca_ucu*0.98
    if pressed[K_RIGHT]:
        buyukluk = buyukluk*1.02
    if pressed[K_LEFT]:
        buyukluk = buyukluk*0.98
    if buyukluk<1:
        buyukluk =1
    if buyukluk>300:
        buyukluk =300
    if firca_ucu<1:
        firca_ucu =1
    if rty != "eraser":
        if firca_ucu>30:
            firca_ucu =30
    if rty == "eraser":
        if firca_ucu>250:
            firca_ucu =250
    #if pressed[K_CAPSLOCK]:
     #   print(time.time()-time_b)
      #  if time.time()-time_b>0.3 and time.time()-time_b<9999999:
       #     if capslock == "off":
        #        capslock = "on"
         #   elif capslock == "on":
          #      capslock = "off"
        #time_b = time.time()

 `      if pressed[K_TAB]:
            background = pygame.image.load('image.png')



    if pressed[K_SPACE]:
        if time.time() - time_a > 0.2:
            durum = durum+1
            if durum == 1:
                pygame.image.save(background, 'image.png')
            if durum >1:
                durum = 0
            time_a = time.time()

    #pygame.display.set_mode(WINDOW_SIZE, pygame.FULLSCREEN)
    for i in pygame.event.get():
        endPos = pygame.mouse.get_pos()
        end_x,end_y = pygame.mouse.get_pos()
        if i.type == pygame.MOUSEBUTTONDOWN:
            background.blit(font.render(yazi, True, (color)), (POS))
            liste = []
            yazi = ""
            r = ""
            italic = False
            bold = False
            altcizgi = False
            pos = pygame.mouse.get_pos()


        elif i.type == pygame.MOUSEBUTTONUP:
            for s in slides:
                s.hit = False

        if r=="rend":
            if i.type == pygame.KEYDOWN:
                character = pygame.key.name(i.key)
                character = character.lower()
                print(character)
                if character == "space":
                    background.blit(font.render(yazi, True, (color)), (POS))
                    liste = []
                    yazi = ""
                    r = ""
                    character = ""
                if character == "return":
                    character = " "
                elif character == "world 0":
                    character = "ü"
                elif character == "world 1":
                    character = "ğ"
                elif character == "world 2":
                    character = "ı"
                elif character == "world 3":
                    character = "ş"
                elif character == "world 4":
                    character = "ö"
                elif character == "world 5":
                    character = "ç"
                print(capslock)
                if capslock == True:
                    character = character.upper()
                if capslock == False:
                    character = character.lower()
                if character == "backspace" or character == "BACKSPACE":
                    liste = liste[:-1]
                    yazi = ''.join(liste)
                else:
                    if len(character) < 2:
                        liste.append(character)
                        yazi = ''.join(liste)

        if i.type == pygame.MOUSEMOTION:
            endPos = pygame.mouse.get_pos()
        if pygame.mouse.get_pressed() == (1, 0, 0):
            POS = pygame.mouse.get_pos()
            x,y = pygame.mouse.get_pos()
            if y>848:
                if x<100:
                    color = (130, 0, 0)
                elif 100<x<200:
                    color = (255, 0, 0)
                elif 200<x<300:
                    color = (255, 193, 0)
                elif 300<x<400:
                    color = (249, 255, 0)
                elif 400<x<500:
                    color = (121, 218, 76)
                elif 500<x<600:
                    color = (0, 187, 81)
                elif 600<x<700:
                    color = (0, 176, 244)
                elif 700<x<800:
                    color = (9, 21, 98)
                elif 800<x<900:
                    color = (125, 0, 161)
                elif 900<x<1000:
                    color = (122, 122, 0)
                elif 1000<x<1100:
                    color = (122, 0, 122)
                elif 1100<x<1200:
                    color = (0, 122, 122)
                elif 1200<x<1250:
                    color = (0, 0, 0)
                elif 1250<x<1300:
                    color = (255,255,255)
                elif 1300<x<1400:
                    sayim = sayim+1
                    if sayim == 1:
                        font = pygame.font.SysFont('arial', 20)
                        pen = Slider("Red", 10, 255, 1, 700)
                        freq = Slider("Green", 1, 255, 1, 750)
                        jmp = Slider("Blue", 10, 255, 1, 800)
                        slides = [pen, freq, jmp]
                        font = pygame.font.SysFont('arial', round(buyukluk))
                    if sayim >1:
                        sayim = 0
                        color = (pen.val,freq.val,jmp.val)
                        slides = []
            else:
                r = "rend"
                durum  =0
        if durum == 1:
            #pygame.draw.rect(background, color, (end_x,end_y,round(firca_ucu),round(firca_ucu)))
            pygame.draw.line(background, color, startPos, endPos, round(firca_ucu))

        if durum >1:
            durum = 0
        startPos = endPos
    screen.blit(background, (0, 0))
    pygame.draw.rect(screen, (255, 255, 255), (sol_bas + 100, 850, 100, 50))
    if durum == 1:
        font = pygame.font.SysFont('arial', 25)
        screen.blit(font.render("Size: "+str(round(int(firca_ucu)))+"", True, (0, 0, 0)), (5, 883))
        if color == (255,255,255):
            rty = "eraser"
            screen.blit(font.render("Eraser", True, (0, 0, 0)), (30, 853))

        else:
            rty = ""
            screen.blit(font.render("Pen", True, (0,0,0)), (5, 853))
            pygame.draw.rect(screen, (color), (sol_bas+150, 850, 50, 25))
        font = pygame.font.SysFont('arial', round(buyukluk))
    else:
        rty = ""
        if r == "rend":
            font = pygame.font.SysFont('arial', 25)
            screen.blit(font.render("Text", True, (0, 0, 0)), (5, 853))
            pygame.draw.rect(screen, (color), (sol_bas + 150, 850, 50, 25))
            screen.blit(font.render("Size: " + str(round(int(buyukluk))) + "", True, (0, 0, 0)), (5, 883))
            font = pygame.font.SysFont('arial', round(buyukluk))

    pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 100, 873, 100, 4))
    if rty != "eraser":
        pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 148, 850, 4, 25))
        pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 100, 873, 100, 4))
    pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 100, 873, 100, 4))
    pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 195, 850, 10, 50))
    pygame.draw.rect(screen, (255, 0, 0), (sol_bas+200, 850, 100, 50))
    pygame.draw.rect(screen, (255, 193, 0), (sol_bas+300, 850, 100, 50))
    pygame.draw.rect(screen, (249, 255, 0), (sol_bas+400, 850, 100, 50))
    pygame.draw.rect(screen, (121, 218, 76), (sol_bas+500, 850, 100, 50))
    pygame.draw.rect(screen, (0, 187, 81), (sol_bas+600, 850, 100, 50))
    pygame.draw.rect(screen, (0, 176, 244), (sol_bas+700, 850, 100, 50))
    pygame.draw.rect(screen, (9, 21, 98), (sol_bas+800, 850, 100, 50))
    pygame.draw.rect(screen, (125, 0, 161), (sol_bas+900, 850, 100, 50))
    pygame.draw.rect(screen, (122, 122, 0), (sol_bas+1000, 850, 100, 50))
    pygame.draw.rect(screen, (122, 0, 122), (sol_bas+1100, 850, 100, 50))
    pygame.draw.rect(screen, (0, 122, 122), (sol_bas+1200, 850, 100, 50))
    pygame.draw.rect(screen, (0, 0, 0), (sol_bas + 1300, 850, 50, 50))
    pygame.draw.rect(screen, (255, 255, 255), (sol_bas + 1350, 850, 50, 50))
    pygame.draw.rect(screen, (255,0,0), (1300, 850, 10, 50))
    pygame.draw.rect(screen, (255, 122, 0), (1310, 850, 10, 50))
    pygame.draw.rect(screen, (255, 255, 0), (1320, 850, 10, 50))
    pygame.draw.rect(screen, (0, 255, 0), (1330, 850, 10, 50))
    pygame.draw.rect(screen, (0, 255, 255), (1340, 850, 10, 50))
    pygame.draw.rect(screen, (0, 122, 255), (1350, 850, 10, 50))
    pygame.draw.rect(screen, (0, 0, 255), (1360, 850, 10, 50))
    pygame.draw.rect(screen, (122, 0, 255), (1370, 850, 10, 50))
    pygame.draw.rect(screen, (255, 0, 255), (1380, 850, 10, 50))
    pygame.draw.rect(screen, (255, 0, 122), (1390, 850, 10, 50))
    pygame.draw.rect(screen, (0, 0, 0), (0, 848, 1400, 4))
    font = pygame.font.SysFont('arial', 100)
    screen.blit(font.render("+", True, (0, 0, 0)), (1330, 840))
    font = pygame.font.SysFont('arial', round(buyukluk))
    if r == "rend":
        font = pygame.font.SysFont('arial', round(buyukluk))
        if altcizgi == True:
            font.set_underline(True)
        elif altcizgi == False:
            font.set_underline(False)
        if bold == True:
            font.set_bold(True)
        elif bold == False:
            font.set_bold(False)
        if italic == True:
            font.set_italic(True)
        elif italic == False:
            font.set_italic(False)
        screen.blit(font.render(yazi, True, (color)), (POS))

    if slides !=[]:

        for s in slides:
            if s.hit:
                s.move()
    for s in slides:
        s.draw()
    if slides != []:
        pygame.draw.rect(screen, (pen.val, freq.val, jmp.val), (1300, 850, 100, 50))
    pygame.display.flip()

Upvotes: 2

Views: 988

Answers (1)

jsbueno
jsbueno

Reputation: 110281

Just put the line bellow in your main loop:

capslock = pygame.key.get_mods() & pygame.KMOD_CAPS

But also, I'd suggest you to take a look into other coding examples on how functions are used, and proceed to some refactoring in this code. Subdividing it into small functions could improve readability a lot in there.

Upvotes: 3

Related Questions