Pygame.MOUSEBUTTONDOWN coordinates are off unless i'm in xwindows

青春壹個敷衍的年華 提交于 2021-02-08 09:48:19

问题


If I load my pygame code from the console, it is reading the touchscreen coordinates wrong, but if I boot into xwindows, it reads them correctly.

I've gone in and calibrated the touch screen, and if I run evtest from the console, I am getting the correct coordinates back. It is only within python that it is returning the incorrect coordinates of the touch.

Is there something i'm missing to tell python how to calibrate?

import sys, pygame
pygame.init()

black = 0, 0, 0
size = width, height = 1280, 800
screen = pygame.display.set_mode(size)


while 1:
        for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                        print(pygame.mouse.get_pos())
        screen.fill(black)

回答1:


When not running under X11, PyGame will leverage SDL both for video (frame buffer) and mouse/touch input (input events). With a touchscreen (as opposed to a traditional mouse) SDL will usually report uncalibrated and (possibly?) un-rotated coordinates unless you make it use TSLIB.

Make sure you have all the needed bits and pieces:

apt-get install tslib libts-bin

Now tell SDL to use it by setting some environment variables before you start your Python script:

export SDL_FBDEV=/dev/fb1
export SDL_MOUSEDRV=TSLIB
export SDL_MOUSEDEV=/dev/input/touchscreen
./my_cool_program.py

Or you can do that "locally" in your Python script - this won't affect environment variables outside of the Python process:

import os
os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ["SDL_MOUSEDRV"] = "TSLIB"
os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"

This should take care of almost everything, including [touch]screen orientation. You should take care of checking the correctness of fbdev and input device paths for you system.

If you want to improve the touchscreen precision you can calibrate it:

TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate

...and you can also test it:

TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test

Calibration data is saved to /etc/pointercal so you don't have to re-calibrate when you reboot the system.



来源:https://stackoverflow.com/questions/26092441/pygame-mousebuttondown-coordinates-are-off-unless-im-in-xwindows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!