How to make my own keyboard layout? - objective-c

Is it possible programatically change keyboard input anywhere , when the program is in tray?
Example.
When user type text in, say, Google Chrome then my program catch the key the user types, and change it to another symbol (in another language)?
A -> ❤
B -> 웃
C -> ✄
etc.
Is it possible? and if answer is YES, then how?

This sounds like a job for a custom keyboard layout.
After installing your keyboard layout, the user need only enable it in the “Language & Text” preference pane and then select it in the Input menu extra.
It's also possible to enable and select an input source (keyboard layouts being a kind of input source) programmatically.

You can do this a number of ways.
You could capture NSEvent key events and change them.
You could override keyDown: in your own view subclass.
And more.
Read up on the Cocoa Text System first
https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html

You can use Quartz event taps to receive all key events first and modify them before allowing them to be processed (or substitute different events entirely). Note that the user will have to enable access by assistive devices in Universal Access preferences in order for event taps to see keyboard events.
You could create, install, and have the user select a custom keyboard layout. You can use the third-party Ukelele program to create and modify keyboard layouts. This doesn't involve code, necessarily. It's just a big state machine for translating key codes to characters.
You can probably accomplish something like this by implementing an input method, too. See the Input Method Kit.

Related

Objective-C bring app to front on key press

I am trying trying to bring an application that is in the background to the foreground by binding to a key-press from the user even when the application does not have focus.
The affect I want is exactly like Quicksilver / Alfred / Spotlight.
Is it possible to do this without having the user mess with any Settings inside Accessibility?
Or is this functionality prevented for security reasons? It seems some people have managed to get around this
Thanks!
PS: I am working off the example from apple with rounded window / transparency:
https://developer.apple.com/library/mac/samplecode/RoundTransparentWindow/Introduction/Intro.html
Registering a Global Event Listener is what you're looking for.
You can then read global key presses and call [NSApp activateIgnoringOtherApps:YES]; on the desired key press.
If you also want the user to be able to customise that shortcut, use MASShortcut.

How to create a background only program that can show a window with text fields

I want to create a program that consists of just a menu in the status bar (no dock icon or menu bar) that, when the user clicks on menu items, opens a window that allows the user to enter text (NSTextField and NSTextView).
I can make the program background only by setting the "Application is background only" property to YES in my Info.plist file, however, when I do this and display a window, firstly the window always appears behind other windows, and secondly I can't type any text into it (text goes to whatever last had focus - usually a source file in Xcode!)
This is definitely not something as simple as the fields are not enabled. Simply changing the "background only" property to NO fixes the issue, but then I get a dock icon and menu bar which I don't want.
Is what I'm trying to do possible or is there something about the background only mode that means my application can never receive text?
If it is possible what do I need to do to fix this?
From the documentation for LSBackgroundOnly:
You can use this key to create faceless background apps. You should also use this key if your app uses higher-level frameworks that connect to the window server, but are not intended to be visible to users.
So when they say “background only”, they mean background only.
The key you want is LSUIElement. Xcode describes this as “Application is agent (UIElement)”.
I personally can't stand Xcode's default behavior of showing me descriptions of some of the keys' meanings. I recommend turning on “Show Raw Keys/Values”; then, Xcode will show you the real keys being used in the dictionary.

How to receive mouse and keyboard events and override default behaviour for Eclipse ITextEditor?

I'd like to create an Eclipse plugin that emulates the behaviour of the vi text editor. This would require changing the way mouse and keyboard events are handled. So, for example if the user presses "h" while in Normal Mode, the cursor should move left, rather than inserting the "h" character into the text buffer. I've found an old mailing list post that describes how to listen to changes in the document, and changes in the presentation, but nothing that describes how to intercept low-level keyboard and mouse events, such that the default behaviour can be overridden. What would be the best way to accomplish this?
One idea would be for a particular active text editor you would want to grab the low-level StyledText widget that is rendering the actual text and also accepting keyboard input and add a KeyListener there.
AbstractTextEditor textEditor = ...
ITextViewer viewer = textEditor.getSourceViewer();
StyledText textWidget = viewer.getTextWidget();
textWidget.addKeyListener(...);

Locking a screen in 10.6

How would I go about locking a screen like Keychain does, meaning preventing all access to Dock, menubar, desktop, etc. Basically just a black screen that I can add a password field to, for the user to return to the desktop? I am well aware of the Carbon method, but I want the NSApplication method because this is an all Cocoa application.
Thanks~
If you can get away with not writing this code yourself, all for the better. It is usually a terrible idea to write your own code to lock the screen, considering the number of vulnerabilities that have been found in screen locking code over the years. If you have a Carbon call that can do it, go ahead and use that... don't worry about the "purity" of your Cocoa code.
However, if you decide to write this yourself, here's what you do:
First, capture all the screens using CoreGraphics. See: http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/QuartzDisplayServicesConceptual/Articles/DisplayCapture.html
Next, create a new NSWindow and put it in front of the window that's used for capturing the screens. You'll have to call a CG function to get the "order" of the black window covering each screen, and order the new window in front of that. Normally, the black window has an order so far forward that everything is behind it. Put a password field in the window. Do NOT use an ordinary text field or write your own code for password input. The password input field has a ton of special code in it so you can't copy text out of it, and other programs can't listen to keystrokes while you're typing into a password field. So use the one that Apple provides.
Last, put the computer in "kiosk mode". This mode allows you to disable alt-tab, user switching, the menubar and dock, and even the ability to force quit. See: http://developer.apple.com/mac/library/technotes/KioskMode/Introduction/Introduction.html
It's not a lot of code, it just uses a few different APIs so you'll spend most of your time bouncing between API docs. I suggest writing the screen lock code as its own application (just add a new application target to your Xcode project) and then put the screen locker inside your application bundle. This used to be (as of 10.4) how Apple Remote Desktop implemented the "Lock Screen" functionality, but I can't find the app anymore.
I believe the Cocoa replacement to the SetSystemUIMode API was not introduced until 10.6.
If you can live with Snow-Leopard-only code, the answer is - setPresentationOptions: on NSApplication.

Binding key combinations to your application

How can I bind a key combination to my vb.net application? I know it has SOMETHING to do with the registry, but I have no earthly idea what or how to go about doing this. I want the user to be able to hit those keys when the app is open and have it execute my function, but not while the app is closed.
Thanks for the help!
If you are using a dialog, then you can put '&' into the text for some controls (buttons, checkboxes, radio buttons, etc) and this will cause Alt plus the next character in the text to be used as an accelerator/shortcut. i.e. "&Open" would activate the Open button if you press Alt+O. "Op&en" would activate if for Alt+e.
Beyond that, as Jason Irwin said, you need to add an event handler to your Form for KeyDown or KeyPress events, and then check if the keypress is the key combination you are interested in. This will only work if the user activates your form (clicks in it to give it the input focus. If they put it behind another window, it will not react to the key presses)
If you don't want to show a form, or want to react to keypresses when you're not the input-focus application, then it gets a bit more complicated (you either need to use a hidden form or a keyboard hook)
edit
OK, it looks like you want a keyboard hook. This looks like a good article.
It depends on what you are trying to do:
If you have a gui application and you want to handle key events then you can do that in a keydown eventhandler
If you want to do more low-level stuff and have an application that will intercept all key strokes (regardless of whether or not the application has focus/is visible) then you need to use pinvoke to hit the win32 apis. I suggest you read the following:
link text
Please let us know what you are trying to do so we can provide better feedback.
Using Google, I found this Keyhook example.
I've worked with keyhooks before, in Delphi WIN32, so I am a bit familiar with them. (Just not in C#.) If you add one to a DLL, all Hell might break loose since most virus scanners will recognise this as malware behaviour. (Especially if you use them in the wrong way, causing them to be injected in each and every process that's running on your system.)
A keyhook will allow key combinations to be captured from other processes, though.
For a solution without programming requirements: Drop a shortcut for the application on your desktop. Edit it, assign a shortcut, close it. Press shortcut to test...