I'm wondering how to add Ctrl V in pygame. I made a text box that will help. I'm just wondering how to search in the data of the computer to know what was the copied word.
CodePudding user response:
If you want to copy the clipboard content use pyperclip
here is a link for installation : pyperclip pip link
you can copy/pasted & edit clipboard content
CodePudding user response:
There is module pygame.scrap to get/set clipboard but I had to use
pygame.scrap.get("text/plain;charset=utf-8").decode()
instead of
pygame.scrap.get(pygame.SCRAP_TEXT)
And I needed to set first
pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
#pygame.scrap.set_mode(pygame.SCRAP_SELECTION)
You can check Ctrl V
in loop for event in pygame.event.get()
with
if (event.key == pygame.K_v) and (event.mod & pygame.KMOD_CTRL):
Minimal working code
import pygame
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLACK = (0, 0, 0)
FPS = 25
# --- main ---
pygame.init()
pygame.scrap.init()
#pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
pygame.scrap.set_mode(pygame.SCRAP_SELECTION)
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_v and event.mod & pygame.KMOD_CTRL:
print('Clipboard:', pygame.scrap.get(pygame.SCRAP_TEXT))
print('Clipboard:', pygame.scrap.get("text/plain;charset=utf-8").decode())
for t in pygame.scrap.get_types():
r = pygame.scrap.get(t)
print('>', t, r)
# --- draws ---
screen.fill(BLACK)
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
# --- end ---
pygame.quit()
In PyGame repo on GitHub I found examples/scrap_clipboard.py
You may also use modules like clipboard