Converting CGKeyCodes for International Keyboards - objective-c

In my application, I need to map a shortcut to the key to the left of the "1" key on the keyboard. On a standard US keyboard, this would be the backtick character (" ` "), which is key code number 50. Unfortunately, international keyboards (the French keyboard, for example) has a different key to the left of the 1 key (the forward slash key "/"), so hard coding that key code would result in unexpected results for users who are not using a US keyboard.
Is there any way to convert a US key code into a key code for international keyboards at runtime, or a way to programatically determine the key code based on the position of the key on the keyboard?

The character on the key to the left of "1" is different on different keyboard layouts, but the virtual key code should be the same. If you look at HIToolbox/Events.h, you can see the constant kVK_ANSI_Grave, which represents the key you're talking about; above the list of constants, there's a comment that suggests that at the virtual key code level, equality means that the physical key is the same, though the scan code might be different and the emitted letter might be different.
In other words: the keyboard driver maps from scan codes to virtual key codes, and the keyboard layout (which you can change in System Preferences) maps from virtual key codes to characters.
This is all potentially wrong; I don't have a non-US keyboard with which to verify these assertions.

Related

Mac Keybindings with German umlauts

I know how to bind a key to a sub in VBA for Mac (Word 2016 etc.). But I can't find a way to do so with German umlauts. I already created some code to bind all 255 variations of wdKey to code, but that didn't get me a solution either. The keys ä, ö, ü on my keyboard never fire any event.
I'd be happy for any advice.
I do not think it is possible.
Some notes:
It looks as if you asked a similar question here in 2012, and the suggestion in there that the problem is that these keys cannot be assigned in Word, only in Mac OS, seems still to be correct. However, that seems to mean that you can only assign a key combination that Mac OS allows you to assign, to an existing Word menu item. So you can use that mechanism to assign ctrl-ö to "Advanced Symbol..." but you cannot use that mechanism to assign ctrl-ö to a Word internal command name and I do not think you can assign ö, on its own, to anything.
In Windows Word, I was able to assign alt-ö, alt-ä and alt-ü to "InsertSymbol" in Word, but the keystrokes Word actually assigned this comand to were the Alt-shifted wdKeyBackSingleQuote, wdKeySingleQuote, and wdKeySemiColon.
Attempting to use the same key assignments in Mac VBA did not work.
On my (UK) keyboard, the actual keys used for ö, ä and ü are SemiColon, Comma, and LeftSquareBracket. Even on the US English keyboard, I think they are SemiColon, SingleQuote, and LeftSquareBracket, so it is not obvious why Word assigns those particular codes. I think you would probably have to understand how DOS and Widnows and Word deal with keyboard scan codes to make sense of it, and on Mac it will of course be different.
For anyone wishing to explore further, you can use VBA to reassign a key without using BuildKeyCode, like this:
Dim i As Integer
i = 1022
KeyBindings.Add KeyCode:=i, _
KeyCategory:=wdKeyCategoryCommand, _
Command:="InsertSymbol"
Some values of i cause an "Invalid Parameter" command, and others a 5346 "Word cannot change the function of the specified key". Most of these values of i probably just represent a "base keystroke" such as "A", eithe ron its own or combined with one or more shifts such as Shift, Alt, etc., but without exploring every single possibile value of i that does not raise an error it is difficult to be certain that there are no other possibilities.

What is the best practice to simulate an ENTER or RETURN using Selenium WebDriver

I came across this solution to my initial problem, which was to simulate an ENTER or RETURN key press using Selenium WebDriver.
However, in my code, I strictly want to use only one of the two WebElement.sendKeys(Keys.ENTER); vs WebElement.sendKeys(Keys.RETURN);.
What is the best practice when doing the same, since there seems to be divided opinion about using enter or return, since both work MOST of the time? In what scenarios would one or the other not work, and is there one which would ALWAYS work?
As a performancewise I do not get any change on both of these,
But yes I know one difference on them
Keys.Enter is used to enter key on the number pad
while
Keys.Return is used to one next to the letters
Generally I have preferred Keys.Enteras sometimes in some browser Keys.Return is not worked for me
Let us analyze Keys.ENTER and Keys.RETURN in details.
Keys.ENTER and Keys.RETURN both are from org.openqa.selenium.Keys, which extends java.lang.Enum<Keys> and implements java.lang.CharSequence
Enum Keys :
Enum Keys is the representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private Use Area) code points, 0xE000-0xF8FF.
Key Codes :
The special keys codes for them are as follows :
RETURN = u'\ue006'
ENTER = u'\ue007'
The implementation of all the Enum Keys are handled the same way.
Hence there is No Functional or Operational difference while working with either sendKeys(Keys.ENTER); or WebElement.sendKeys(Keys.RETURN); through Selenium.
Enter Key and Return Key :
On computer keyboards, the Enter (or the Return on Mac OSX) in most cases causes a command line, window form, or dialog box to operate its default function. This is typically to finish an "entry" and begin the desired process, and is usually an alternative to pressing an OK button.
The Return is often also referred as the Enter and they usually perform identical functions; however in some particular applications (mainly page layout) Return operates specifically like the Carriage Return key from which it originates. In contrast, the Enter is commonly labelled with its name in plain text on generic PC keyboards.
Wiki References : Enter Key Carriage Return
As yourself, I failed to find a good explanation to this question online so I tested it myself using this Keyboard Events tester.
driver.get("https://dvcs.w3.org/hg/d4e/raw-file/tip/key-event-test.html");
WebElement textArea = driver.findElement(By.id("input"));
textArea.sendKeys(Keys.ENTER);
textArea.sendKeys(Keys.RETURN);
As a result I've got this output (this is Keys.ENTER followed by Keys.RETURN):
So it seems like there is no difference between these two options.

Private are mapping of unichar in Cocoa

In NSEvent in key down event it has characters property and it has a character like 'a' as the first character when you hit a key 'a'.
When I hit down-arrow key I get 63233 as a decimal expression of the first character.
I wonder what is the number and I found that 63233 = 0xF701 is in a private area in Unicode according to http://en.wikipedia.org/wiki/Private_Use_(Unicode) .
What I want to know is how and where they are defined. Is there any document which lists up all the character mappings used in Cocoa??
The NSText class reference contains the character mappings of common command and modifier keys. The Down Arrow key is specifically defined as NSDownTextMovement = 0x16. A more complete list can be found in HIToolbox, a sub-framework of Carbon in Events.h

How to check if text in a UITextField matches a specific pattern?

I have a UITextField and I need to check, as the user types, if the text they have entered in the textfield so far matches a specific pattern.
More specifically, I need the text to match the pattern ####-##-## where # is any digit 0-9 and - is a dash (note that this is NOT a phone number or email). For example, the entry 1990-12-09 matches, 1990:12:09 does not match and 1990-12 DOES match because it has not yet violated the pattern (even though the text does not yet completely match the pattern).
How should I approach this? Ideally I would not have to hard code in a series of if statements .
The difficulty is that I want to check if the text in the textfield matches this pattern, as the user types. I don't want to just check it at the end.
I'm thinking that regular expressions are probably the way, but I'm not experienced enough at them to know if they hold the solution.
You could set the keyboard type to myTextField.keyboardType = UIKeyboardTypeNumberPad that why you are limiting the type of input.
Then this post should help answer your formatting question: UITextField format in xx-xx-xxx

Identifying a substitution cipher random key. (English text)

input:
Crypted English normal text (A-Z) using a random generated substitution cipher.
output:
key
ideas:
read the whole text storing in some arrays the frequencies for each character/bigram/trigram and comparing them to:
http://en.wikipedia.org/wiki/Letter_frequencies
http://en.wikipedia.org/wiki/Bigram
http://en.wikipedia.org/wiki/Trigram
cons: letters/bigrams/trigrams with close percentage (like "c" and "u")
my software should be able to guess the max. possible characters from the crypted text (minimum 2000 characters).
I have to guess at least 18-20 letters.
questions:
is there a way/known algorithm to guess all the characters => full key ?
or can you give me some useful references or advices on how I could improve the whole guessing process ?
I think you're on the right track. The only way you could recover the full key would be if the all characters (or all-1) are present in the plain text.
I'd be thinking along the lines of making some statistical guesses and then statictically checking the results for the plaintext Bigrams/Trigrams which result. Or checking whole words (if you know where the word boundaries are) against a word list.