I have followed a slightly different approach to try and avoid the problems you cite.
Firstly create a dictionary in a calculated property comprising all the character codes:
Public Shared Property diAscii as xojo.core.Dictionary
Get
If mdiAscii = Nil Then
//Note, standard dictionaries are not case sensitive, so need to generate case sensitive version
mdiAscii = New Xojo.Core.Dictionary
AddHandler mdiAscii.compareKeys, AddressOf diCaseSensitivity
Dim modKeys() As Integer = Array(0, 2, 8, 10) //none, shift, option, shift+option
For Each modValue As Integer In modKeys
Dim modCode As Integer = modValue * 128
For i As Integer = 0 To 127
//apply the keyCode for each of the base ascii characters in the keyboard
//only option and shift keys modify the recieved ascii
Dim key As String = RemoteControlMBS.MacTextForKeyCode(i, 3, modValue)
mdiAscii.value(key.ToText) = modCode + i
Next
Next
End
Return mdiAscii
End Get
Set
End Set
End Property
Note: this has to be a xojo.core.dictionary made case sensitive using the compare keys event. The modKeys applied are to capture shift and option keys and have to do all the conversion to Text from String etc.
Next in the KeyDown and/or KeyUp methods add this code:
Private Shared Sub System_KeyDown(v as variant)
Dim key As String = v
Dim keyChar As Integer = key.Asc
if targetwin32 then
Break
Elseif targetmachO Then
Dim di As xojo.core.dictionary = diAscii
//get the modified code from the dictionary - ie ascii code plus 128 * modifiers
Dim ModifiedCode As Integer = di.Lookup(key.toText, 63) //leave 63 = ? as default
//Get the modifier values
Dim modCode As Integer = ModifiedCode/128 //ie the integer value - 0, 2, 8, 10
//set the modifier keys
SetModKeys(modCode, true)
Dim virtualKey As Integer = ModifiedCode - modCode*128 //remove the modifiers
Dim b As Boolean = RemoteControlMBS.MacPressKey(keyChar, virtualKey, True)
Elseif TargetLinux Then
Break
Else
Break
End If
th_clicks.Resume
End Sub
This grabs the virtual key and ascii key from the dictionary and applies them to the MBS.macPressKey.
The setModKeys method simply applies the decoded mod keys on or off. In my methods I turn them on before KeyDown and off after KeyUp.
Private Shared Sub SetModKeys(modCode as integer, down as boolean)
Select Case modCode
Case 0
//ignore
Case 2
Call RemoteControlMBS.MacPressShiftKey( down )
Case 8
Call RemoteControlMBS.MacPressOptionKey( down )
Case 10
Call RemoteControlMBS.MacPressShiftKey( down )
Call RemoteControlMBS.MacPressOptionKey( down )
Else
Break
End Select
End Sub
Hopefully this method will be independent of keyboard attached to the device - though I am sure there are code improvements to be made (especially with the auto, text, string conversions).
It seems to work OK with basic characters but has problems with the delete key and multi-key diacritics like Ă©.