scrap_clipboard.py :  » Game-2D-3D » Pygame » pygame-1.9.1release » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Pygame 
Pygame » pygame 1.9.1release » examples » scrap_clipboard.py
#!/usr/bin/env python
"""
Demonstrates the clipboard capabilities of pygame.
"""
import os

import pygame
from pygame.locals import *
import pygame.scrap as scrap
import StringIO

def usage ():
    print ("Press the 'g' key to get all of the current clipboard data")
    print ("Press the 'p' key to put a string into the clipboard")
    print ("Press the 'a' key to get a list of the currently available types")
    print ("Press the 'i' key to put an image into the clipboard")

main_dir = os.path.split(os.path.abspath(__file__))[0]

pygame.init ()
screen = pygame.display.set_mode ((200, 200))
c = pygame.time.Clock ()
going = True

# Initialize the scrap module and use the clipboard mode.
scrap.init ()
scrap.set_mode (SCRAP_CLIPBOARD)

usage ()

while going:
    for e in pygame.event.get ():
        if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
            going = False

        elif e.type == KEYDOWN and e.key == K_g:
            # This means to look for data.
            print ("Getting the different clipboard data..")
            for t in scrap.get_types ():
                r = scrap.get (t)
                if r and len (r) > 500:
                    print ("Type %s : (large buffer)" % t)
                else:
                    print ("Type %s : %s" % (t, r))
                if "image" in t:
                    namehint = t.split("/")[1]
                    if namehint in ['bmp', 'png', 'jpg']:
                        f = StringIO.StringIO(r)
                        loaded_surf = pygame.image.load(f, "." + namehint)
                        screen.blit(loaded_surf, (0,0))


        elif e.type == KEYDOWN and e.key == K_p:
            # Place some text into the selection.
            print ("Placing clipboard text.")
            scrap.put (SCRAP_TEXT, "Hello. This is a message from scrap.")

        elif e.type == KEYDOWN and e.key == K_a:
            # Get all available types.
            print ("Getting the available types from the clipboard.")
            types = scrap.get_types ()
            print (types)
            if len (types) > 0:
                print ("Contains %s: %s" % (types[0], scrap.contains (types[0])))
                print ("Contains _INVALID_: ", scrap.contains ("_INVALID_"))

        elif e.type == KEYDOWN and e.key == K_i:
            print ("Putting image into the clipboard.")
            scrap.set_mode (SCRAP_CLIPBOARD)
            fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb')
            buf = fp.read ()
            scrap.put ("image/bmp", buf)
            fp.close ()

        elif e.type in (KEYDOWN, MOUSEBUTTONDOWN):
            usage ()
    pygame.display.flip()
    c.tick(40)




www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.