folder Tahribat.com Forumları
linefolder Python
linefolder Python Pygame İle Joystick‘İ Mouse Olarak Kullanma (Linux)



Python Pygame İle Joystick‘İ Mouse Olarak Kullanma (Linux)

  1. KısayolKısayol reportŞikayet pmÖzel Mesaj
    cnr437
    cnr437's avatar
    Banlanmış Üye
    Kayıt Tarihi: 03/Nisan/2007
    Erkek

    Python ile Pygame modülünü kullarak, usb joystick'lerin analog kısmının linux altında X için mouse olarak kullanılmasını sağlar, ilginç bişi, ilgilenenlerin işine yarabilir,


    from Xlib import X,ext,display
    import pygame
    import time

    # Dependencies
    # - - - - - - -
    # sudo apt-get install python-xlib python-pygame

    scale=10        #mouse movement scalar
    xpos,ypos=0,0    #start position
    keycode = 30

    keys = (
            {24:'q', '25':'w', }
    )

    """
    buttons={
    0:"A"
    1:"O"
    2:"X"
    3:"|_|"
    4:"LEFT_2"
    5:"RIGHT_2"
    6:"LEFT_1"
    7:"RIGHT_1"
    8:"SELECT"
    9:"START"
    10:"LEFT_AXIS_BUTTON"
    11:"RIGT_AXIS_BUTTON"
    }
    """

    pygame.init()
    j=pygame.joystick.Joystick(0)
    j.init()

    d = display.Display()
    scr=d.screen()
    root=scr.root

    width=scr.width_in_pixels
    height=scr.height_in_pixels

    #--debug code

    #print j.get_numaxes() , "axe(s)."
    #print j.get_numbuttons() , "button(s)."
    #print j.get_numhats() , "hat(s)."

    def mouse_click(button):
        ext.xtest.fake_input(d,X.ButtonPress,button)
        d.sync()
        ext.xtest.fake_input(d,X.ButtonRelease,button)

    def press(key):
        ext.xtest.fake_input(d,X.KeyPress,key)
        d.sync()
        ext.xtest.fake_input(d,X.KeyRelease,key)

    def mouse_position():
        position=root.query_pointer()._data
        return position["root_x"], position["root_y"]


    try:
        while True:
            pygame.event.pump()

            if j.get_axis(0):
                #print "x axis"        #--debug code
                xpos,ypos=mouse_position()
                xpos=xpos+int(j.get_axis(0)*scale)
                if xpos<0: xpos=0
                elif xpos>width: xpos=width
                root.warp_pointer(xpos,ypos)

            if j.get_axis(1):
                #print "y axis"        #--debug code
                xpos,ypos=mouse_position()
                ypos=ypos+int(j.get_axis(1)*scale)
                if ypos<0: ypos=0
                elif ypos>height: ypos=height
                root.warp_pointer(xpos,ypos)

            if j.get_button(0):
                #print "Mouse left click."    #--debug code
                press(24)
                time.sleep(0.1)

            if j.get_button(1):
                #print "Mouse left click."    #--debug code
                press(X.Lock)
                time.sleep(0.1)

            elif j.get_button(10):
                #print "Mouse left click."    #--debug code
                mouse_click(1)
                time.sleep(0.1)

            elif j.get_button(11):
                #print "Mouse right click."    #--debug code
                mouse_click(3)
                time.sleep(0.1)

            elif j.get_axis(4):
                if j.get_axis(4) < 0:
                    mouse_click(4)
                else:
                    mouse_click(5)
                time.sleep(0.05)

            #print xpos,ypos        #--debug code
            d.sync()
            time.sleep(0.02)
    except KeyboardInterrupt:
        print "error"
        j.quit()


     


    Bizim olduğumuz her yerde herşey bizim yüzümüzden olmuştur. Ben benim amk bana bişey olmasın!
Toplam Hit: 1244 Toplam Mesaj: 1