mouse movement as arrows up/down/left/right
-
I'm trying to implement a mouse movement to a Frame to simulate keyboard direction keys. I some relative info and I tried the following:
class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow,self).__init__(parent) self.old_x = QCursor.pos().x() self.old_y = QCursor.pos().y() self.setupUi(self) self.frame.setMouseTracking(True) self.frame.installEventFilter(self) .... def eventFilter(self, source, event): if event.type() == QtCore.QEvent.MouseMove: #if event.buttons() == QtCore.Qt.NoButton: # print("Simple mouse motion") if event.buttons() == QtCore.Qt.LeftButton: print("Left click drag") new_x = event.x() new_y = event.y() if new_x > self.old_x: print("direction : right") self.matrix, done = logic.right(self.matrix) if done: self.stateCheck() elif new_x < self.old_x: print("direction : left") self.matrix, done = logic.left(self.matrix) if done: self.stateCheck() elif new_y > self.old_y : print("direction : down") elif new_y < self.old_y: print("direction : up") self.old_x = new_x self.old_y=new_y return super(QFrame, self.frame).eventFilter(source, event)
This doesn't work as expected, and the movement changes directions most of the times, probably because when press the mouse button there is a very little movement to a different direction.
What I want to achive, is the functionality of keyboard direction keys. Can be done with the above approach? -
@dancaer69
Not enough information how often/how much your things do/do not move.At a guess, you might want to build some "tolerance" in. I might move right a lot, but perhaps have a tiny bit of left at, say, the end. You may have to look for how much the movement is, and ignore small moves. You might also want a timer-delay, so that multiple movements are combined. Again, depends on more information as to what is/is not working right.
-
Thanks, you had right. Needed to do something like that, but I didn't know how to do it. I tried QGestures which was my first choise, but I didn't find any information for pyqt and I didn't manage to make it work either. Finally I found moosegestrures library which does more that I want, but again no much info about that. It has some pygame examples though, and with those I managed to do what I wanted(probably not with an elegant way).
So here is how I did it:class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow,self).__init__(parent) self.old_x = QCursor.pos().x() self.old_y = QCursor.pos().y() self.setupUi(self) self.frame.setMouseTracking(True) self.frame.installEventFilter(self) self.points[] .... def eventFilter(self, source, event): if event.type() == QtCore.QEvent.MouseMove: #if event.buttons() == QtCore.Qt.NoButton: # print("Simple mouse motion") if event.buttons() == QtCore.Qt.LeftButton: if len(self.points) > 2: startx, starty = self.points[0][0], self.points[0][1] for i in range(len(self.points)): self.points[i] = (self.points[i][0] - startx, self.points[i][1] - starty) self.points.append((event.localPos().x(), event.localPos().y())) print(self.points) #self.points = [] elif event.type() == QtCore.QEvent.MouseButtonRelease: if event.button() == QtCore.Qt.LeftButton: print("Released!") self.mouseDown = False strokes = moosegesture.getGesture(self.points) strokeText=str(strokes[-1]) #print(strokeText) if strokeText == "R": self.matrix, done = logic.right(self.matrix) if done: self.stateCheck() if strokeText == "L": self.matrix, done = logic.left(self.matrix) if done: self.stateCheck() if strokeText == "U": self.matrix, done = logic.up(self.matrix) if done: self.stateCheck() if strokeText == "D": self.matrix, done = logic.down(self.matrix) if done: self.stateCheck() return self.frame.eventFilter(source, event)