Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples not working on mac.. #39

Closed
willwade opened this issue May 30, 2014 · 4 comments
Closed

examples not working on mac.. #39

willwade opened this issue May 30, 2014 · 4 comments

Comments

@willwade
Copy link
Contributor

I may be being stupid..

from pymouse import PyMouse
from pykeyboard import PyKeyboard

m = PyMouse()
k = PyKeyboard()

dir(k)  #Use dir() on a PyKeyboard instance
k.numpad_keys.viewkeys()
k.function_keys

gives:
AttributeError: 'PyKeyboard' object has no attribute 'numpad_keys'
(and if I comment the numpad line)
AttributeError: 'PyKeyboard' object has no attribute 'function_keys'

(running latest zip on mac)

The initial readme could do with a one liner on how to send a normal key as well as a function key..

@willwade
Copy link
Contributor Author

Ok so I'm getting this to work - kinda..

k.press_key('command')
k.press_key('shift')
k.press_key('3')
k.release_key('command')
k.release_key('shift')
k.release_key('3')

that actually just sends shift 3 (£ on my layout) - not creating a screenshot (apple shift 3 is a system wide shortcut for creating a screenshot). I'm guessing what its doing is sending command key and not holding it down with the other two.. (i.e. its just tapping it).

I'm grasping at straws here (i.e. I can't figure this out) but is that because there is only a sticky state for shift - and not the other command keys?

(UPDATE: I see that I should have read the previous issue #38 . Apologies for adding to the list..)

@SavinaRoja
Copy link
Owner

Actually I think you are demonstrating some facets of how the Mac implementation is incomplete. I am not so knowledgeable when it comes to Mac and don't have one to test on. It looks like it could use some fleshing out.

@willwade
Copy link
Contributor Author

Im wondering if its something to do with it needing kCGEventFlagMaskCommand (I'm still getting my head around this but it looks like the modifier keys need a different approach: http://stackoverflow.com/questions/15206825/cmdoptiond-simulation-in-cocoa/15207244#15207244) -

I've done some test code and this works..

from Quartz import *
from AppKit import NSEvent

keyCode = 8; # C - i.e. we are going to do a a command(apple)+c

source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
eventDown = CGEventCreateKeyboardEvent(source, keyCode, YES);
CGEventSetFlags(eventDown, kCGEventFlagMaskCommand);
eventUp = CGEventCreateKeyboardEvent(source, keyCode, NO);
CGEventPost(kCGSessionEventTap, eventDown);
CGEventPost(kCGSessionEventTap, eventUp);
CFRelease(eventUp);
CFRelease(eventDown);
CFRelease(source);

NB: It does work but it also leads to a segmentation fault. I'm not sure why. It does though tend to suggest you can save pretending to press all the modifier keys - just the keys that you need with the kCGEventFlagMaskCommand set on them..

(also are you aware of this @SavinaRoja : https://github.com/abarnert/pykeycode ? - it may be more reliable than the lookup table - particularly when detecting keystrokes?)

Happy to pay for a remote mac hosting plan for a little while if it helps ;)

@willwade
Copy link
Contributor Author

willwade commented Jun 1, 2014

I've written an adaption to the current code which seems to be more reliable.. https://github.com/willwade/PyUserInput/blob/master/pykeyboard/mac.py - I would do a pull request but I'm not 100% confident its that much better - the eval is horrible and there is a shift problem (see this gist for an example: https://gist.github.com/willwade/52ccf1455772d6597c30)

Note that for the examples to work, run them in the terminal then switch to the finder.. more example shortcuts to test: http://support.apple.com/kb/HT1343

UPDATE: It does now work - I realised it was only sending one modifier so now it can send multiple including a shift modifier. I'm still a little concerned this may mess up the standard shift configuration - let me know if you want me to do a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants