Adding Ctrl+V / Ctrl+C functionality
frankvp11 opened this issue · 5 comments
I was wondering if you could add control copy + paste functionality into this class? I have some base code that would work to a degree, however I am not experienced enough to add it to your class. I've left the example of how to do so below.
pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
#event loop
events = pygame.event.get()
for event in events:
if (event.type == pygame.QUIT):
pygame.quit()
if (event.type == pygame.KEYDOWN):
# checking for ctrl + V
if event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
# adding text to a text box called promptBox, where .text is the thing that holds the text
pasted_text = pygame.scrap.get("text/plain;charset=utf-8").decode()
promptBox.text = promptBox.text[:-1]
promptBox.text += pasted_text
I did this using a different inputBox class, which is why i did .text, however I don't know how I could apply this functionality to your class, so I was wondering if you had any ideas?
Actually, never mind I dug through and found out how to do the functionality. Perhaps you could add this as an example or potentially even still add this to your class? I've linked below the github repo so you can check it out yourself https://github.com/frankvp11/openAItesting
Though your code is correct. It is giving so many bugs like after pasting if I press up or down key or page up and down, backspace does not work sometimes and sometimes crashes due to this. Also remove that -1 from your code as it copies last letter again. So it should look like this
promptBox.input_string = ( promptBox.input_string[:promptBox.cursor_position] + pasted_text + promptBox.input_string[promptBox.cursor_position:])
I tried to add this functionality into my class but it creates bugs like I mentioned above and tried to fix but cannot do. So I guess I will not add.
Btw nice use of api, haha
Thanks, haha. The reason I had that -1 in the code at the time was because when you do Ctrl+V it gets read as a unicode thing.
Now you can paste text.
Actually bugs were caused when you copy multiple lines. Clipboard adds /r at the end of the line. So I had to remove that in every line of pasted text.
Also if you kept the Ctrl + V pressed, it crashes due to attribute error.
So I asked ChatGPT by giving that code and error to fix it. And it fixed it hahahahaha.
By replacing event.mod
by pygame.key.get_mods()
So it looks like this
elif event.key == pygame.K_v and pygame.key.get_mods() & pygame.KMOD_CTRL:
Nice! I love to see it.