How do I show a cursor on a Canvas (as meant as an indication for typing)?
Everything on a canvas is “drawn” during the PAINT event. You do not/cannot “type” on it.
You capture keystrokes, do some math and “draw” the appropriate character in the approrpriate place.
Alex Restrepo has a CustomEditField that does exactly that
And our Formatted Text Control does that too. And if you’re building for Cocoa you should really look at using the TextInputCanvas plugin (from Xojo) because that’s the only way to get special characters (think , , to name a few things) into it. The normal events don’t work like they used to on Cocoa (they still work in Windows and Linux).
OK, I knew that I have to draw the text myself, but I was thinking about the cursor…
Thnks…
Alex Restrepo’s custom edit field does not allow me to draw a custom background. So I drew it myself.
How do I get the keydown characters on the Canvas? I see the canvas keydown event but how to get the typed characters in the g.drawstring method?
[quote=55388:@Alexander van der Linden]Alex Restrepo’s custom edit field does not allow me to draw a custom background. So I drew it myself.
How do I get the keydown characters on the Canvas? I see the canvas keydown event but how to get the typed characters in the g.drawstring method?[/quote]
Restrepos edit field is canvas based…why can’t you add a background? I’ve implemented such from the control no problem…look at the paint event if you need help would be glad to lend…no point reinventing the wheel if it can be avoided. The control also answers your previous question demonstrating ‘how-to’ perfectly
As Bob already alluded to IF you want to handle text input properly under Cocoa you REALLY need to start with the text input canvas
Cocoa’s text input system is wildly different from Carbons so making it work right really requires starting with the free TextInput plugin
There is a PaintOver event… it paints on the foreground… How did you do that?
[quote=55486:@Norman Palardy]As Bob already alluded to IF you want to handle text input properly under Cocoa you REALLY need to start with the text input canvas
Cocoa’s text input system is wildly different from Carbons so making it work right really requires starting with the free TextInput plugin[/quote]
Thanks Norman,
I will try with Alex’ customeditfield first.
Norman do you have a link to the Text Input Canvas plugin by chance? I compiled it with Xcode 5 and it works great. I just see a ton of event handlers that aren’t familiar to me.
Thanks!
http://documentation.xojo.com/index.php/Community_Resources
First entry under Open Source
Or here GitHub - xojo/TextInputCanvas: TextInputCanvas is a plugin for the Xojo programming environment that allows developers to implement custom text input controls with international input support.
Hi Norman, how exactly do I install the plugin? I downloaded it several weeks ago but gave up trying because I was unsure what I was supposed to do with the source code files. I tried to drop the zip in with the extension changed, just drop in the src folder and build in Xcode but none of that seemed to work and the plugin was never loaded in Xojo. Can you point me in the right direction? Thanks
If you downloaded the zip file from Github it is JUST the source code for the plugin so you’d need to compile it to make the actual plugin. This will require Xcode on OS X, VS on Windows or gcc on Linux.
I thought there was a built version posted somewhere as well but it seems I was mistaken.
Ok that worked thanks. I must have been doing something wrong before…
[quote=55486:@Norman Palardy]As Bob already alluded to IF you want to handle text input properly under Cocoa you REALLY need to start with the text input canvas
Cocoa’s text input system is wildly different from Carbons so making it work right really requires starting with the free TextInput plugin[/quote]
I guess the question is, why not make the canvas control in the framework work right with text input?
is the answer that because a new framework is coming and it;s too big a job to be worth doing now for how many are affected?
BTW I assume this applies to the string passed into keydown on any control besides those OS controls specifically for text input? So for a listbox , the issue would be in listbox.KeyDown but not in Listbox.CellKeyDown? (not that I can think of case offhand where it would matter in listbox.KeyDown)
Here’s an email I wrote when I initially was investigating this.
[quote=Joe Ranieri]Under Carbon there was kEventRawKeyDown
which, as you might imagine, was simply a notification that a key was pressed. It was the lowest level keyboard handling event that you could get and gave you the virtual keyboard code, modifiers, and an initial translation to characters. Since this is before it had gone through the Text Services Manager, the system for handling input methods and dead keys, it wasn’t suitable for text input but worked fine for games and a handful of other specialized uses.
For general text input, applications were expected to use kEventTextInputUnicodeForKeyEvent
. This automatically gave you Unicode key events, showing the ‘bottomline’ input window for text input methods that needed more than dead keys. For example, Opt-E E wouldn’t show the bottomline input window but using the Katakana input method would. However, implementing only kEventTextInputUnicodeForKeyEvent
and none of the other TSM related Carbon events was sort of a halfway measure for applications to be able to pick up Unicode support without a lot of work.
Since the bottomline input window was a poor user experience, applications were encouraged to implement inline input by implementing more of the Carbon events. Inline input basically means that your application is responsible for handling the display of the incomplete text in the control itself and that you didn’t need the bottomline input window.
Under Cocoa, however, things are different. This bottomline input window doesn’t exist and the only thing you can get for free are raw key down events (corresponding to NSResponder's keyDown:
method). Just like under Carbon, this isn’t really meant for text input. If you wish to support text input, your view needs to implement the NSTextInputClient
protocol. This protocol has quite a few required methods and a general lack of good high-level documentation, but isn’t too hard once you understand the concepts.
So, back to Xojo itself… The Carbon framework handled kEventTextInputUnicodeForKeyEvent
on your behalf, getting you free text input at the expense of using the bottomline input window. The Cocoa framework, on the other hand, has to use the raw key down event because nothing else exists for free. The key passed into the KeyDown event is the translation of the virtual key code to a character without going through any input methods. So really, the meaning of the KeyDown event has changed from “InsertText” to “KeyDown” under the Cocoa framework and nothing can really be done about that on our end.
Unfortunately, NSTextInputClient
can’t be implemented in pure Xojo code. You’d need to write a plugin that exposes an NSView
that implements it and calls through to events in Xojo. We did this to support input into the IDE’s code editor and it works out fairly well. You get support for all of the input methods and things like the dictionary panel for free. With a little extra work, you can also make services work.
If you don’t care about anyone other than English users using a normal keyboard layout and just want to support dead keys, you can get at the underlying NSEvent
that triggered the KeyDown event with NSApplication's currentEvent
method. Once you have the NSEvent
, you can get all of the parameters required to call UCKeyTranslate
and have that do your key translation. I really don’t recommend going down this route.
Related Carbon documentation:
Related Cocoa documentation:
Here is a link to my dropbox for the compiled TextInputCanvas Plugin.
Ugh I hate when you go back to reread your own post and realized you left out a key word… Norman sorry I forgot the word DOCUMENTATION in my post above
I already compiled it, but I couldn’t find any docs on the docs xojo site.
Do you have any links for any documentation for the Text Input Canvas Plugin? I have been playing with it and I can’t figure out how to use it so far
Thanks!!
I found this link that Norman and Joe spoke of the documentation of this plugin.
Mmm… interesting topic… Is there a demo project or something available? I compiled the plugin with Xcode and put it in the Plugins folder. Where to start from there?