how to use keybd_event to simulate an extended ascii keycode

i am trying to get my program to simulate pressing, for example, the keys Alt+0233 (NumPad) which makes the character é. but i cant get it to work. the program i am writing will be something like a form filler and needs to use some way of simulating the keystrokes for the odd letter such as ö (alt+0246) or ç (alt+0231) to be displayed in a form in another program. i got all the rest of the standard keyboard letters and numbers to work just fine. i hope someone can help.
LVL 1
MoglorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
To display the char

Just do

Text1.Text = Chr(231)
Text1.Text = Chr(233)
0
MoglorAuthor Commented:
the characters need to be displayed in an entirely different program. as if i wanted my program to write the text in an address field in an address book or something. Im using the keybd_event function to simulate pressing the keys of the keyboard, and it works great, if all the characters are standard letters and numbers. its when i want to press the alt key followed by 4 number pad keys that it messes up.
0
EDDYKTCommented:
Have you tried

    keybd_event 17, 0, 0, 0  ' alt down
    keybd_event vbKeyNumpad0, 0, 0, 0
    keybd_event vbKeyNumpad0, 0, 2, 0
    keybd_event vbKeyNumpad2, 0, 0, 0
    keybd_event vbKeyNumpad2, 0, 2, 0
    keybd_event vbKeyNumpad4, 0, 0, 0
    keybd_event vbKeyNumpad4, 0, 2, 0
    keybd_event vbKeyNumpad6, 0, 0, 0
    keybd_event vbKeyNumpad6, 0, 2, 0
    keybd_event 17, 0, 2, 0  ' alt up
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike TomlinsonMiddle School Assistant TeacherCommented:
I see what you mean Moglor.

This should put the extended character into our textbox but it doesn't.  Typing ALT-0246 manually in the textbox produces the character.

Interesting...

Still looking,

Idle_Mind

' Code follows...
Option Explicit

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const VK_ALT = &H12

Private Const VK_NUMPAD0 = &H60
Private Const VK_NUMPAD1 = &H61
Private Const VK_NUMPAD2 = &H62
Private Const VK_NUMPAD3 = &H63
Private Const VK_NUMPAD4 = &H64
Private Const VK_NUMPAD5 = &H65
Private Const VK_NUMPAD6 = &H66
Private Const VK_NUMPAD7 = &H67
Private Const VK_NUMPAD8 = &H68
Private Const VK_NUMPAD9 = &H69

Private Const KEYEVENTF_KEYUP = &H2

Private Sub sendExtendedChar(charCode As String)
    Dim a As Integer
    Dim curChar As String

    charCode = Trim(charCode)
    If Not IsNumeric(charCode) Then
        Exit Sub
    End If

    ' ALT down
    keybd_event VK_ALT, 0, 0, 0
   
    ' Send each digit from NumPad
    For a = 1 To Len(charCode)
        curChar = Mid(charCode, a, 1)
        Select Case curChar
            Case "0"
                keybd_event VK_NUMPAD0, 0, 0, 0
                keybd_event VK_NUMPAD0, 0, KEYEVENTF_KEYUP, 0
               
            Case "1"
                keybd_event VK_NUMPAD1, 0, 0, 0
                keybd_event VK_NUMPAD1, 0, KEYEVENTF_KEYUP, 0
               
            Case "2"
                keybd_event VK_NUMPAD2, 0, 0, 0
                keybd_event VK_NUMPAD2, 0, KEYEVENTF_KEYUP, 0
               
            Case "3"
                keybd_event VK_NUMPAD3, 0, 0, 0
                keybd_event VK_NUMPAD3, 0, KEYEVENTF_KEYUP, 0
               
            Case "4"
                keybd_event VK_NUMPAD4, 0, 0, 0
                keybd_event VK_NUMPAD4, 0, KEYEVENTF_KEYUP, 0
               
            Case "5"
                keybd_event VK_NUMPAD5, 0, 0, 0
                keybd_event VK_NUMPAD5, 0, KEYEVENTF_KEYUP, 0
               
            Case "6"
                keybd_event VK_NUMPAD6, 0, 0, 0
                keybd_event VK_NUMPAD6, 0, KEYEVENTF_KEYUP, 0
               
            Case "7"
                keybd_event VK_NUMPAD7, 0, 0, 0
                keybd_event VK_NUMPAD7, 0, KEYEVENTF_KEYUP, 0
               
            Case "8"
                keybd_event VK_NUMPAD8, 0, 0, 0
                keybd_event VK_NUMPAD8, 0, KEYEVENTF_KEYUP, 0
               
            Case "9"
                keybd_event VK_NUMPAD9, 0, 0, 0
                keybd_event VK_NUMPAD9, 0, KEYEVENTF_KEYUP, 0
               
        End Select
    Next a

    ' ALT up
    keybd_event VK_ALT, 0, KEYEVENTF_KEYUP, 0
End Sub

Private Sub Command1_Click()
    Text1.SetFocus
    sendExtendedChar "0246"
End Sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I added this to the project:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Debug.Print KeyAscii
End Sub

and nothing ever reaches the textbox.  If I comment out the two lines that depress/release the Alt key, then the keystrokes arrive.

Idle_Mind
0
CimperialiCommented:
Something that was originally wrote by Tim Cottee should work...
Option Explicit
'Const VK_F3 As Long = &H72
Const VK_ALT As Long = 18
Const VK_SHIFT As Long = &H10
'Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()
    Text1.SetFocus
    keybd_event VK_ALT, 0, 0, 0  ' press alt
    keybd_event Asc("b"), 0, 0, 0   ' press b
    keybd_event Asc("b"), 0, KEYEVENTF_KEYUP, 0 'release "b"
    keybd_event VK_ALT, 0, KEYEVENTF_KEYUP, 0 'release alt
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Debug.Print "letter pressed was " & Chr$(KeyCode) & " and Alt was " & CBool((Shift And vbAltMask) = vbAltMask)

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    Debug.Print "letter released was " & Chr$(KeyCode) & " and Alt was " & CBool((Shift And vbAltMask) = vbAltMask)
End Sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Cimperiali,

That is exactly what I and othrs have already submitted.  The problem lies in the fact that Moglor is trying to produce an extended ascii character.  Look at the post closely.

To produce an extended character you have to hold down the ALT key and then type the code for the character on the Number Pad.

Your code will work great for ALT-B but something else is going on here with the extended keys.

Try it for yourself.

Idle_Mind
0
MoglorAuthor Commented:
ok good, Idle_Mind has hit apon my problem exactly. im glad someone else could reproduce my situation and varify that i wasnt simply making a mistake somewhere. i figure the alt key is being pressed but the numberpad keys arnt registering properly.

 i just had a thought, maybe the keys are being pressed to fast for windows to figure out what youre trying to do. im gonna go experiement some more.
0
MoglorAuthor Commented:
nope, delaying between key presses doesnt do anything
0
EDDYKTCommented:
Try this it works

Option Explicit
'Const VK_F3 As Long = &H72
Private Const VK_ALT As Long = 18
Private Const VK_SHIFT As Long = &H10
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()

    Dim nSCan As Long
   
    Text1.SetFocus
    keybd_event vbKeyMenu, MapVirtualKey(vbKeyMenu, 0), 0, 0  ' alt down
    keybd_event vbKeyNumpad0, MapVirtualKey(vbKeyNumpad0, 0), 0, 0
    keybd_event vbKeyNumpad0, MapVirtualKey(vbKeyNumpad0, 0), 2, 0
    keybd_event vbKeyNumpad2, MapVirtualKey(vbKeyNumpad2, 0), 0, 0
    keybd_event vbKeyNumpad2, MapVirtualKey(vbKeyNumpad2, 0), 2, 0
    keybd_event vbKeyNumpad4, MapVirtualKey(vbKeyNumpad4, 0), 0, 0
    keybd_event vbKeyNumpad4, MapVirtualKey(vbKeyNumpad4, 0), 2, 0
    keybd_event vbKeyNumpad6, MapVirtualKey(vbKeyNumpad6, 0), 0, 0
    keybd_event vbKeyNumpad6, MapVirtualKey(vbKeyNumpad6, 0), 2, 0
    keybd_event vbKeyMenu, MapVirtualKey(vbKeyMenu, 0), 2, 0  ' alt up
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "Text1_KeyDown " & KeyCode, Shift
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Debug.Print "Text1_KeyPress " & KeyAscii
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "Text1_KeyUp " & KeyCode, Shift
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CimperialiCommented:
EDDYKT, you made it!

Idle_Mind, you are right: I did not read the whole, but as I did I realized what I posted was not correct.

See you all on the Net,
Cesare I.

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Nice EDDYKT!

If we combine we our code we get this generic solution:

Regards,

Idle_Mind

' Code Follows
Option Explicit

Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()
    Text1.SetFocus
    sendExtendedChar "0246"
End Sub

Private Sub sendExtendedChar(charCode As String)
    Dim a As Integer
    Dim curChar As String

    charCode = Trim(charCode)
    If Not IsNumeric(charCode) Then
        Exit Sub
    End If

    ' ALT down
    keybd_event vbKeyMenu, MapVirtualKey(vbKeyMenu, 0), 0, 0  ' alt down
   
    ' Send each digit from NumPad
    For a = 1 To Len(charCode)
        curChar = Mid(charCode, a, 1)
        Select Case curChar
            Case "0"
                keybd_event vbKeyNumpad0, MapVirtualKey(vbKeyNumpad0, 0), 0, 0
                keybd_event vbKeyNumpad0, MapVirtualKey(vbKeyNumpad0, 0), 2, 0
               
            Case "1"
                keybd_event vbKeyNumpad1, MapVirtualKey(vbKeyNumpad1, 0), 0, 0
                keybd_event vbKeyNumpad1, MapVirtualKey(vbKeyNumpad1, 0), 2, 0
               
            Case "2"
                keybd_event vbKeyNumpad2, MapVirtualKey(vbKeyNumpad2, 0), 0, 0
                keybd_event vbKeyNumpad2, MapVirtualKey(vbKeyNumpad2, 0), 2, 0
               
            Case "3"
                keybd_event vbKeyNumpad3, MapVirtualKey(vbKeyNumpad3, 0), 0, 0
                keybd_event vbKeyNumpad3, MapVirtualKey(vbKeyNumpad3, 0), 2, 0
               
            Case "4"
                keybd_event vbKeyNumpad4, MapVirtualKey(vbKeyNumpad4, 0), 0, 0
                keybd_event vbKeyNumpad4, MapVirtualKey(vbKeyNumpad4, 0), 2, 0
               
            Case "5"
                keybd_event vbKeyNumpad5, MapVirtualKey(vbKeyNumpad5, 0), 0, 0
                keybd_event vbKeyNumpad5, MapVirtualKey(vbKeyNumpad5, 0), 2, 0
               
            Case "6"
                keybd_event vbKeyNumpad6, MapVirtualKey(vbKeyNumpad6, 0), 0, 0
                keybd_event vbKeyNumpad6, MapVirtualKey(vbKeyNumpad6, 0), 2, 0
               
            Case "7"
                keybd_event vbKeyNumpad7, MapVirtualKey(vbKeyNumpad7, 0), 0, 0
                keybd_event vbKeyNumpad7, MapVirtualKey(vbKeyNumpad7, 0), 2, 0
               
            Case "8"
                keybd_event vbKeyNumpad8, MapVirtualKey(vbKeyNumpad8, 0), 0, 0
                keybd_event vbKeyNumpad8, MapVirtualKey(vbKeyNumpad8, 0), 2, 0
               
            Case "9"
                keybd_event vbKeyNumpad9, MapVirtualKey(vbKeyNumpad9, 0), 0, 0
                keybd_event vbKeyNumpad9, MapVirtualKey(vbKeyNumpad9, 0), 2, 0
               
        End Select
    Next a

    ' ALT up
    keybd_event vbKeyMenu, MapVirtualKey(vbKeyMenu, 0), 2, 0  ' alt up
End Sub
0
MoglorAuthor Commented:
thanks for the hard work guys
0
EDDYKTCommented:
Glad we can help.

Merry X'mas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.