Reputation: 73
I am trying to track the movement of the mouse on the screen while pressing right-click and saving the movement into a 2D shape (either .dwg or other) into a new file. What is the best way to approach this?
I have looked into PyMouse and briefly into PyGame but so far. But, because I still have a limited understanding of coding, I don't understand how to actually use them and create a running application.
I have tried these simple examples for basic function of PyMouse (https://github.com/pepijndevos/PyMouse/wiki/Documentation) but don't know how to get from here to tracking a user mouse movement.
I'd appreciate any kind of tips for this!
Upvotes: 1
Views: 1260
Reputation: 463
For the mouse tracking event, you can use the if event.type == pygame.MOUSEBUTTONDOWN:
statement.
The full code below:
import pygame,sys,numpy
pygame.init()
display = (1500, 900)
screen = pygame.display.set_mode(display)
pygame.display.set_caption("Shape")
draw = False
size = (15,15)
run = True
shape = []
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: #Detect right-click; left-click is 1
draw = True
if event.type == pygame.MOUSEBUTTONUP and event.button == 3: #Detect release right-click
draw = False
#Here to save the file of 2d shape
shape = []
screen.fill((0,0,0))
#Draw the shape
if draw == True:
shape.append(pygame.mouse.get_pos())
for i in shape:
screen.fill((255,255,255), (i, size))
pygame.display.flip()
pygame.quit()
sys.exit()
I think there is a way to save list as a 2d file. Check out https://gis.stackexchange.com/questions/52705/how-to-write-shapely-geometries-to-shapefiles and it might help you. Just add the saving-file process after the #Here to save the file of 2d shape
comment.
I will be working on the saving-file part, but this is the best I can get for now.
Upvotes: 1