Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1512

Alt+NumPad input for Unicode TextBox with surrogate pair support

$
0
0
When using Alt+NumPad for Unicode input I get a bogus character in Notepad/Notepad++ and all other Unicode TextBox implementations that I tried. WordPad and InkEdit, on the other hand. works OK, including surrogate pairs.

Test summary:
Alt+128512 (&H1F600) WordPad/InkEdit , Notepad/Notepad++/TextBoxW/ucText Nothing
Alt+173569 (&H2A601) WordPad/InkEdit , Notepad/Notepad++TextBoxW/ucText ☺ (&H263A, 9786)
Alt+931 (&H03A3) WordPad/InkEdit Σ , Notepad/Notepad++/TextBoxW/ucText ú (&HFA, 250)

Here is sample code that overrides the internal Alt+NumPad behavior:
1. Assumes you have a subclassed Unicode TextBox with source code that exposes Translate Accelerator.
2, Make sure NumLock is On before testing.
3. Tested with Segoe UI Regular.

Code:

Option Explicit

Private mbDeleteChar As Boolean

Private Function KeyPressed(ByVal KeyCode As KeyCodeConstants) As Boolean
  KeyPressed = CBool((GetAsyncKeyState(KeyCode) And &H8000&) = &H8000&)
End Function

Private Function ToSurrogatePair(ByVal i As Long) As String
  Dim Hi              As Integer, Lo As Integer
  On Error GoTo ErrHandler
  i = i - &H10000
  Hi = i \ &H400 + &HD800
  Select Case Hi
    Case &HD800 To &HDBFF
      Lo = i Mod &H400 + &HDC00
      Select Case Lo
        Case &HDC00 To &HDFFF
          'Debug.Print Hex(Hi), Hex(Lo)
          ToSurrogatePair = ChrW$(Hi) & ChrW$(Lo)
      End Select
  End Select
ErrHandler:
End Function

'Build string in Translate Accelerator WM_SYSKEYDOWN.
Friend Function TranslateAccel(pMsg As Msg) As Boolean
  Static mSysWord  As String
 
    Case WM_SYSKEYDOWN
      If KeyPressed(vbKeyMenu) Then 'Alt Pressed
        Select Case pMsg.wParam
          Case vbKeyNumpad0 To vbKeyNumpad9
            mSysWord = mSysWord & ChrW$(pMsg.wParam - 48)
        End Select
      End If
    Case WM_CHAR
      If Len(mSysWord) Then
        Dim i                As Long
        Dim s                As String
        On Error Resume Next
        i = CLng(mSysWord)
        Select Case i
          Case &HD800& To &HDBFF& 'Skip Reserved
          Case Is <= &HFFFF& '0 - 65535
            s = ChrW$(i)
          Case Is <= &H10FFFF 'Unicode max value
            s = ToSurrogatePair(i)
        End Select
        If Len(s) Then
          SelText = s 'Insert as SelText
          mSysWord = vbNullString 'Reset
          mbDeleteChar = True 'To delete bogus WM_CHAR that Alt+ generated internally.
        End If
        On Error GoTo 0
      End If

'Finally delete the bogus character that appears in WM_CHAR when Alt is released.
myWndProc:
    Case WM_CHAR
      If mbDeleteChar Then
        mbDeleteChar = False
        wParam = 0
      End If

Similar code was tested in Krools TextBoxW (use wParam in lieu pf pMsg.wParam and KeyChar = 0 in WindowProcControl) and it appears to be working OK here. TextBoxW.Zip atttached.

TextBoxW.zip
Attached Files

Viewing all articles
Browse latest Browse all 1512

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>