Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17011

How do get macOS trackpad finger position directly with Python and Objective-C

$
0
0

I'm trying to get the positions of the macOS trackpad. I have looked at this post:know the position of the finger in the trackpad under Mac OS X

and I used the Objective-C code in it and added some extra code:

- (id)initWithFrame:(NSRect)frameRect {    self = [super initWithFrame:frameRect];    if (!self) return nil;   /* You need to set this to receive any touch event messages. */    [self setAcceptsTouchEvents:YES];   /* You only need to set this if you actually want resting touches.    * If you don't, a touch will "end" when it starts resting and    * "begin" again if it starts moving again. */    [self setWantsRestingTouches:YES]    return self;}/* One of many touch event handling methods. */- (void)touchesBeganWithEvent:(NSEvent *)ev {    NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseBegan inView:self];   for (NSTouch *touch in touches) {      /* Once you have a touch, getting the position is dead simple. */      NSPoint fraction = touch.normalizedPosition;      NSSize whole = touch.deviceSize;      NSPoint wholeInches = {whole.width / 72.0, whole.height / 72.0};      NSPoint pos = wholeInches;      pos.x *= fraction.x;      pos.y *= fraction.y;      NSLog(@"%s: Finger is touching %g inches right and %g inches up "            @"from lower left corner of trackpad.", __func__, pos.x, pos.y);    }}@interface MyTouchHandler : NSView // Other declarations...- (CGFloat)getXPos;- (CGFloat)getYPos;@end@implementation MyTouchHandler// Other method implementations...- (CGFloat)getXPos {    return pos.x }- (CGFloat)getYPos {    return pos.y }@end

And I tried to access the pos.x and pos.y in Python directly with the Objective-C library:

import objcfrom Foundation import NSObject, NSLogclass MyTouchHandler(NSObject):    # Other methods...    # Define methods to access posX and posY    @objc.typedSelector(b"f@:")    def getXPos(self):        return self.getXPos()    @objc.typedSelector(b"f@:")    def getYPos(self):        return self.getYPos()# Create an instance of the Objective-C classhandler = MyTouchHandler.alloc().init()# Access posX and posY from Pythonx_pos = handler.getXPos()y_pos = handler.getYPos()print(b"Accessed posX: %g, posY: %g", x_pos, y_pos)

However, I'm getting this error:

Traceback (most recent call last):  File "/Users/xxxxxxxxx/Desktop/osu_pad/main.py", line 20, in <module>    x_pos = handler.getXPos()  File "/Users/xxxxxxxxx/Desktop/osu_pad/main.py", line 10, in getXPos    return self.getXPos()  File "/Users/xxxxxxxxx/Desktop/osu_pad/main.py", line 10, in getXPos    return self.getXPos()  File "/Users/xxxxxxxxx/Desktop/osu_pad/main.py", line 10, in getXPos    return self.getXPos()  [Previous line repeated 496 more times]RecursionError: maximum recursion depth exceeded

I'm stuck and I don't really know what to do. And I also think this error doesn't really make sense. What are the necessary steps to fix this issue I am encountering.


Viewing all articles
Browse latest Browse all 17011

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>