I am trying to write an application similar to Mac ID, but with a little bit different options.
As far as I can see all the similar applications out there store the password in the Keychain and simply type it in the password field on the login screen.
Here is my original code written based on Apple docs and Robot:
#include <ApplicationServices/ApplicationServices.h>
void Press(int key);
void Release(int key);
void Click(int key);
int main() {
Press(56);
Click(6);
Release(56);
}
void Press(int key) {
// Create an HID hardware event source
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
// Create a new keyboard key press event
CGEventRef evt = CGEventCreateKeyboardEvent(src, (CGKeyCode) key, true);
// Post keyboard event and release
CGEventPost (kCGHIDEventTap, evt);
CFRelease (evt); CFRelease (src);
usleep(60);
}
void Release(int key) {
// Create an HID hardware event source
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
// Create a new keyboard key release event
CGEventRef evt = CGEventCreateKeyboardEvent(src, (CGKeyCode) key, false);
// Post keyboard event and release
CGEventPost (kCGHIDEventTap, evt);
CFRelease (evt); CFRelease (src);
usleep(60);
}
void Click(int key) {
Press(key);
Release(key);
}
It's supposed to output a 'Z', but it outputs a 'z'. According to the documentation modifier keys must be "pressed" as well.
What's wrong with it?
Then after googling a day I found this SO answer which seemed to solve my SHIFT issue (although I am not 100% about the correctness of this solution), but I can't seem to be able to output characters like "ÁÉÍÓÜÖ".
How can I do that? Is it possible in the first place?
I know this is already more than one question, but I am still trying to solve the same problem and I couldn't find a perfect solution so far, so I guess it would be nice for others to have this answer.
Thank you very much in advance, any help is appreciated.
Its possible to get any character you want. You are almost on correct path. I think you just need to specify what unicode character you are interested in. So instead of sending key if you send unicode and use kVK_Space as keycode you should have what you need and before posting the event you can call following code one that event object
if (unicode){
unichar str[2] = {0};
str[0] = (unichar)unicode;
memcpy(str, &unicode, 4);
CGEventKeyboardSetUnicodeString(theEvent, 1, str);
}