Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

PygameinputboxesseveralcharsareintookafteronekeyboardclickorclickisnotdetectedHotkeysGUI

Hi the basic idea of the script was to create graphical interface for creating hotkeys for openning websites. I wanted hotkeys to be saved so that whenever I want to use the script hotkeys that I already asigned can be used.

In the input boxes(website and name), several chars are intook after one keyboard click or click is not detected. I tride pygame.wait, but all i got is either to fast or to slow key click intake.

Sometimes after closing gui hotkeys are still detected I need to close IDE to get rid of them, I would like my final version of script to be converted into .exe, that is why after closing .exe it is important not to have hotkeys still working.

Other issue is that after adding hotkey, websites are open 3 -4 times as a result of one hotkey execution. After closing script and running it again (hotkeys read from csv) this problem doesn't occur.

I am using spyder, python 2.7 and win10.

I would use some help. Thank you in advance ;)

import pygame
import webbrowser
import keyboard
import csv

keyList = []
pygame.init()
clock = pygame.time.Clock()

size = width, hight = 600,400

blue = (72,120,170)
red = (160,29,33)

black = (0,0,0)

brightBlue = (10,203,238)
brightRed = (233,29,41)

COLOR_A = pygame.Color('dodgerblue2')
COLOR_I = pygame.Color('lightskyblue3')

window = pygame.display.set_mode(size)
pygame.display.set_caption('Web_hotkey')

bck = pygame.image.load('bg.jpg')
bck = pygame.transform.scale(bck,size)


class Hotkeys:
    def __init__(self,str_name,hotkey,website):
        self.combination = hotkey
        self.name= str_name
        self.adress = website



def readCsv(listKey= keyList):
    with open('hotkeys.csv','r') as f:
        reader = csv.reader(f,delimiter=' ')
        for row in reader:
             print(row)
             listKey.append(Hotkeys(row[0],row[1],row[2]))
    f.close();



def writeCsv(listKey=keyList):
    with open('hotkeys.csv','wb') as f:
        writer = csv.writer(f, delimiter=' ')
        for i in range(0,len(listKey)):
            line = listKey[i].name,listKey[i].combination,listKey[i].adress
            writer.writerow(line)

def hot2web(listKey=keyList):

    for i in range(0,len(listKey)):
        print(listKey[i].combination)
        keyboard.add_hotkey(listKey[i].combination, webbrowser.open, args=[listKey[i].adress])    


class Button():
    def __init__(self, txt, location,bg,bgAct, fg=black, size=(500,50), font_name= "comicsansms", font_size=20):
        self.color = bg
        self.bg = bg
        self.bgAct = bgAct
        self.fg = fg
        self.size = size

        self.font = pygame.font.SysFont(font_name, font_size)
        self.txt = txt
        self.txt_surf = self.font.render(self.txt, 1, self.fg)
        self.txt_rect = self.txt_surf.get_rect(center=[s//2 for s in self.size])

        self.surface = pygame.surface.Surface(size)
        self.rect = self.surface.get_rect(center=location)

    def draw(self):
        self.mouseover()

        self.surface.fill(self.bg)
        self.surface.blit(self.txt_surf, self.txt_rect)
        window.blit(self.surface, self.rect)

    def mouseover(self):
        self.bg = self.color
        pos = pygame.mouse.get_pos()
        if self.rect.collidepoint(pos):
            self.bg = self.bgAct  


addhotkey = Button('Add hotkey',(300,345),blue,brightBlue)
buttons = [addhotkey]

def clickCheck():
    pos = pygame.mouse.get_pos()
    for button in buttons:
        if button.rect.collidepoint(pos):
            return True
        else:
            return False


class InputBox: 
    def __init__(self,x,y,width,height,comment,box_type,font_size=15,font_style='comicsansms'):


        text = ''
        self.font = pygame.font.SysFont(font_style,font_size)
        self.txt = text
        self.txt_surf = self.font.render(text,True,black)
        self.com_surf = self.font.render(comment,True,black)
        self.com_rect = pygame.Rect(x-(width/2),y-(height/2)-20,width,height)
        self.txt_rect = pygame.Rect(x-(width/2),y-(height/2),width,height)
        self.color = COLOR_I
        self.active = False
        self.box_type = box_type


    def eventHandler(self,event):

        if event.type ==pygame.MOUSEBUTTONDOWN:
            mouse = pygame.mouse.get_pos()

            if self.txt_rect.collidepoint(mouse):
               self.active = not self.active
            else:
                self.active = False

            if self.active == True:
                self.color = COLOR_A
            else:
                self.color = COLOR_I

        if self.active:
              if event.type ==pygame.KEYDOWN:
                    if self.box_type == 'hotkey':
                        self.txt = keyboard.read_hotkey(False)
                    if event.key == pygame.K_BACKSPACE:
                        self.txt = self.txt[:-1]
                    else:

                        self.txt = self.txt + event.unicode
              self.txt_surf = self.font.render(self.txt, True, self.color)




    def draw(self,window):
        window.blit(self.txt_surf,(self.txt_rect.x+5,self.txt_rect.y+5))
        window.blit(self.com_surf,self.com_rect)
        pygame.draw.rect(window,self.color,self.txt_rect,2)


nickBox = InputBox(125,300,150,25,'Website name','nick',)
hotkeyBox = InputBox(300,300,150,25,'press key combination','hotkey')
webBox = InputBox(475,300,150,25,'Input website adress','web_adress')
input_boxes =[nickBox,hotkeyBox,webBox]


#Intake of hotkeys and websites previously asigned
flag = 1
while flag:
    readCsv()
    hot2web()
    flag = 0
#Asignment of new hotkeys 
loop = True     
while loop:


    for event in pygame.event.get():
         if(event.type == pygame.QUIT):
             loop = False

             pygame.display.quit()
             pygame.quit()
         if event.type == pygame.MOUSEBUTTONDOWN:
            for button in buttons:
                click = clickCheck()


    clock = pygame.time.Clock()
    window.blit(bck,(0,0))
    for button in buttons:
        button.draw()
    for box in input_boxes:
        box.draw(window)


    nick = ''
    hotkey= '' 
    web_adress= ''
    for box in input_boxes:
        box.eventHandler(event)
        variable = box.txt
        box_type = box.box_type
        if  variable != None:
            if box_type == 'nick':
                nick = variable
            elif box_type == 'hotkey':
                hotkey = variable
            else:
                web_adress = variable

        if nick != '' and hotkey != '' and web_adress != '' and click:

            click = False
            keyList.append(Hotkeys(nick,hotkey,web_adress))
            hot2web(keyList)
            writeCsv()
            nick = ''
            hotkey = ''
            web_adress = ''


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

Comments