Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a function that convert a keycode to a scancode?
I have a function/process that send scancode, I use this so that I can Send Keys to a Citrix Receiver. But every time I use that code, first I need to open a Visual Basic KeyBoard Program that determine what Scan Code is pressed.

VB6 Keyboard Hook code
VB
Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If nCode = HC_ACTION Then
        CopyMemory kbstruct, ByVal lParam, LenB(kbstruct)
        Debug.Print kbstruct.scanCode
    End If
    KeyboardProc = CallNextHookEx(Hook, nCode, wParam, lParam)
End Function


This is a function I used in sending key to a citrix receiver (cannot send ENTER/TAB any special key)
VB
TypeInput("Test Text with ARROW UP", Keys.Up, 72)
TypeInput("Normal Text", 0, 0)

Private Sub Pause(Optional ByVal millisecondsTimeout As Integer = 100)
    Application.DoEvents()
    Threading.Thread.Sleep (millisecondsTimeout)
End Sub

Private Sub TypeInput(ByVal input As String, ByVal wVk As Short, ByVal wScan As Short)
    SendKeys.SendWait(input)
    Pause()
    If (wVk > 0 And wScan > 0) Then
        Dim inp As New INPUT
        inp.Type = InputType.Keyboard
        inp.u.ki = New KEYBDINPUT
        inp.u.ki.wVk = wVk
        inp.u.ki.wScan = wScan
        inp.u.ki.time = 0
        inp.u.ki.dwFlags = KEYEVENTF.KeyDown
        inp.u.ki.dwExtraInfo = IntPtr.Zero
        SendInput(1, inp, Marshal.SizeOf(GetType(Input)))
    End If
End Sub


Thanks a lot.
Posted
Comments
Sergey Alexandrovich Kryukov 13-May-15 15:29pm    
There is no such thing as convert to scan code. This is not how it all works. What's the actual problem?
And SendKeys is bad, and DoEvent is bad.
—SA
hansoctantan 14-May-15 6:29am    
so what should i do is use the SendInput, but every character has a wScan
Keys.Up = 38 and its Scan code is 72. The above code don't have a problem, what I want is not manually check the scancode of a key.
Sergey Alexandrovich Kryukov 14-May-15 10:31am    
What do you want to achieve? Scan codes are known. Why not handling KeyDown? (KeyUp?)
—SA
Sergey Alexandrovich Kryukov 14-May-15 10:31am    
What do you want to achieve? Scan codes are known. Why not handling KeyDown? (KeyUp?)
—SA
hansoctantan 14-May-15 14:20pm    
i guess this is a similar for what im searching for
Dim kc As KeysConverter = New KeysConverter()
Dim sKey As String = kc.ConvertToString(Keys.O)

instead of string i need to convert that to scancode

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900