Built-in function for converting between unicode characters and virtual keycodes in Cocoa? - iphone

Is there a way to convert a unicode character to a Mac virtual keycode? (without building my own table?) It looks like on Windows there is VkKeyScanEx, but I'm not aware of a similar function for Cocoa on OS X.
I'm actually trying to do this for the iPad. I want to convert character taken from the keyboard and convert them into key codes, since the iPad keyboard won't supply keycodes.

The ShortcutRecorder project on GoogleCode has an NSValueTransformer subclass for converting strings to keycodes and vice versa, but I'm not sure if it'll work on iOS. It's a great place to start looking, though.

I'm interested in the reason why it needs to be tagged iPhone/iPad — surely you can do all the conversion in OS X? Also, the iPhone/iPad "keyboard" is fundamentally a text input method (see UITextInput) —it's not that it "won't supply keycodes"; there simply aren't any (and what keycode/modifiers should it supply when you tap "A", hold for a bit, and pick a random accented version?).
If you're going to do this, test it on a variety of (odd) input methods on both the iPad and OS X. If there's an API to insert a string, do so (but this might not work so well for games which read scan codes...). You could even write a custom input method extension which accepted Unicode strings.
It's debatable what should happen when a Dvorak VNC client types to a QWERTY VNC server...
I'll end with a tangential story:
A little over a year ago (before I got an iPhone), I got a N810. If anything, it makes a half-decent SSH/VNC client and has a decent keyboard.
Except it's not a standard keyboard. 1 is Fn-Q and ! is Fn-A, but when I type Fn-A to get "!", the VNC server ends up typing "1". Typing Shift-Fn-A gives me the "!" I was looking for (I think Shift-Fn-Q also works).
Something, somewhere, parses the character "!", decides that it has the same scan code as "1", and types the scan code for 1 with no modifier. It could automatically hold down Shift. It might even be able to insert a string. Instead, it just fails.

Related

In Python (or any language) what does an "upper" function do to Hindi, Amharric and other non-Latin character sets?

Subject says it all. Been looking for an answer, but cannot seem to find it.
I am writing a web app that will store data in a database and also have language files translated into a wide variety of character sets. At various moments, the text will be presented. I want to control presentation such as spurious blank spaces at the beginning and end of strings. Also I want to ensure some letters are upper or lower case.
My question is: what happens in upper/lower case functions when the character set only has one case?
EDIT Sub question: Are there any unexpected side effects to be aware of?
My guess is that you simply get back the one and only character.
EDIT - Added Description
The main reason for asking this question is that I am writing a webapp that will be distributed and run on machines in remote areas with little or no chance to fix "on-the-spot" bugs. It's not a complicated webapp, but will run with many different language char sets. I want to be certain of my footing before releasing the server.
First of all the upper() and lower() method in python can be applied to Hindi, Amharric and non-letter character sets.
For instance will the upper() method converts the lowercase characters if an equivalent uppercase of this char exists. If not, then not.
Or better said, if there is nothing to convert, it stays the same.

Is there a way to dynamically change the encoding for terminal input so that "a" is ऄ and "b" is ब (in unicode) and so on...?

I would like to write in arbitrary fonts in the terminal, such as Chinese, Devangari, Mayan Hieroglyphs (a font that is not even part of unicode yet), etc.
I would like to press "a" to get ऄ, etc., basically I say "enter encoding DEVANAGARI" and now "a" is ऄ, etc. Or I say "enter encoding MAYAN" and "a" is some "private unicode space" glyph, etc. How can I do this? Can I set it dynamically somehow, maybe using Swift (for Mac) if I had a Mac app running in the background?
For example, I would imagine like this:
$ change-script DEVANAGARI
$ a
# replaced with
$ ऄ
# etc
How can I do this in the Terminal app, or in any app for that matter?
This way I can use the ASCII keyboard to write in arbitrary fonts even if they aren't in unicode.
Answering the title question: yes. This can be accomplished by changing the active keyboard. Apple supplies a bunch (including many QWERTY-style for various scripts that roughly map the English/latin letters to (rough) equivalents in the script). As noted in a comment, there are tools available to create custom layouts if the supplied ones are not sufficient.
As to the question of doing it programatically, that's trickier. The accepted answer to this (old) SO question suggests that you can programatically switch keyboards (presumably: installed ones).
But by a close read of your question and follow-up, it seems like you basically want to create/modify the active keyboard dynamically (?). I'd be surprised if anything like that is supported, but I'm also not sure why it would be necessary to do that if you have the ability to switch programatically.

Converting emoji from hex code to unicode

I want to use emojis in my iOS and Android app. I checked the list of emojis here and it lists out the hex code for the emojis. When I try to use the hex code such as U+1F600 directly, I don't see the emoji within the app. I found one other way of representing emoji which looks like \uD83D\uDE00. When using this notation, the emoji is seen within the app without any extra code. I think this is a Unicode string for the emoji. I think this is more of a general question that specific to emojis. How can I convert an emoji hex code to the Unicode string as shown above. I didn't find any list where the Unicode for the emojis is listed.
It seems that your question is really one of "how do I display a character, knowing its code point?"
This question turns out to be rather language-dependent! Modern languages have little trouble with this. In Swift, we do this:
$ swift
Welcome to Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1). Type :help for assistance.
1> "\u{1f600}"
$R0: String = "😀"
In JavaScript, it is the same:
$ node
> "\u{1f600}"
'😀'
In Java, you have to do a little more work. If you want to use the code point directly you can say:
new StringBuilder().appendCodePoint(0x1f600).toString();
The sequence "\uD83D\uDE00" also works in all three languages. This is because those "characters" are actually what Unicode calls surrogates and when they are combined together a certain way they stand for a single character. The details of how this all works can be found on the web in many places (look for UTF-16 encoding). The algorithm is there. In a nutshell you take the code point, subtract 10000 hex, and spread out the 20 bits of that difference like this: 110110xxxxxxxxxx110111xxxxxxxxxx.
But rather than worrying about this translation, you should use the code point directly if your language supports it well. You might also be able to copy-paste the emoji character into a good text editor (make sure the encoding is set to UTF-8). If you need to use the surrogates, your best best is to look up a Unicode chart that shows you something called the "UTF-16 encoding."
In Delphi XE #$1F600 is equivalent to #55357#56832 or D83D DE04 smile.
Within a program, I use it in the following way:
const smilepage : array [1..3] of WideString =(#$1F600,#$1F60A,#$2764);
JavaScript - two way
let hex = "😀".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);

Scala Random.nextString(int) returning question marks

Whenever I use Random.nextString(int), I get a String of questions marks (??????). I've tried using creating an instance of Random and using a seed, but nothing works. I am using Scala 2.10.5. Anyone know what the issue is?
In most terminals, when a character is not displayable (there are a lot of existing characters, and you cannot remotely hope to have them all in the font used by your terminal), it will print a question mark instead.
Because the string is random, you are very likely to have the vast majority of them be non displayable (and thus rendered as a sequence of question marks).
So the strings are valid, they are indeed random (not just a series of question marks), and this is all just a rendering issue. You can easily check that their content really is different each time by displaying the character codes (something like println(myString.map(_.toInt)) will do).

Unicode Code Point for Command Key Combinations

Can someone please tell me how to determine the unicode character point of a multi-key combination that includes the "command" key? For example, if a user presses the "command" key and "1" key on the keyboard at the same time, what is the unicode character representation for that?
Maybe I'm searching on the wrong thing, but I am not able to locate this in the character maps, keyboard references, or unicode tables I find. I can sort out other key combinations (e.g. shift-1) as there is an obvious character output of "!" that I can look up and find that it is U+0021. When I go to character maps or applications the command key always seems to take an action rather than output a character result to screen.
My app is for iOS, which I would expect to be the same as Mac OS X in terms of the unicode code point. All of the iOS APIs that provide access to the keyboard see it as a source of Unicode characters. Thus the reason I am trying to detect keystrokes this way.
Thanks.
Keyboard codes are basically independent of character codes.
While (as you mention) many keys have standard mappings to standard ASCII codes, it is up to the application to decide what to do with them.
Some input API's may be widely used on a particular OS, and some applications (e.g., terminal emulators) may be used as a common input method for a class of tasks, but there is no universal standard.
Obligatory wikipedia link for Unicode input.
You can't. There simply are no Unicode codepoints that correspond to Command + some-other-character.
The same is true of Shift, by the way. The fact that your computer happens to map certain combinations to certain Unicode codepoints does not imply that Unicode specifies such mappings, or that mappings exist for every combination of keys, or that those mappings are the same for everyone else. I use two keyboards every day; one of them maps Shift+3 to #, the other maps it to £. This is decided by the operating system, not by Unicode. If you tried to detect a Shift+3 keypress by listening for #, your program would seem to me to be broken half the time.
This is a perfect example of an XY question. You don't really care about Unicode -- what you really want to know is how to detect keypresses with the Command modifier on iOS. You should just have asked how to do that! There is probably an API that does exactly what you need that you have simply missed, because you were concentrating on your assumption that the solution would involve Unicode -- and there are probably numerous iOS experts who have not bothered to read this question at all, because they thought your problem related to Unicode rather than iOS.
Simple answer: no.
You haven't told us what sort of computer you are using. Mapping a key press to a Unicode code point is operating system specific, and then it depends on the locale that is active.