如何在pygame中添加ctrl+v?

发布于 2025-01-20 12:32:02 字数 1399 浏览 0 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铜锣湾横着走 2025-01-27 12:32:02

有模块 pygame.scrap.scrap 获取/设置剪贴板,但(在Linux上)我必须使用

pygame.scrap.get("text/plain;charset=utf-8").decode()

代替

pygame.scrap.get(pygame.SCRAP_TEXT)

,我需要先设置

pygame.scrap.init()
pygame.scrap.set_mode(pygame.SCRAP_CLIPBOARD)
#pygame.scrap.set_mode(pygame.SCRAP_SELECTION)

检查 ctrl+v

if (event.key == pygame.K_v) and (event.mod & pygame.KMOD_CTRL):

pygame.event.get()中使用最小工作代码

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()

您可以在 pygame repo在github上我也找到了示例


您也可以使用模块,例如 clipboard

There is module pygame.scrap to get/set clipboard but (on Linux) 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 also examples/scrap_clipboard.py


You may also use modules like clipboard

饭团 2025-01-27 12:32:02

如果要复制剪贴板内容使用 pyperclip

以下是安装链接:

您可以复制/粘贴&编辑剪贴板内容

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文