?
Solved

how to use keybd_event to simulate an extended ascii keycode

Posted on 2003-12-02
14
Medium Priority
?
2,009 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Moglor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
14 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9859112
To display the char

Just do

Text1.Text = Chr(231)
Text1.Text = Chr(233)
0
 
LVL 1

Author Comment

by:Moglor
ID: 9859180
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9859771
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9859865
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9859902
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
 
LVL 5

Expert Comment

by:Cimperiali
ID: 9859941
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9860018
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
 
LVL 1

Author Comment

by:Moglor
ID: 9860500
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
 
LVL 1

Author Comment

by:Moglor
ID: 9860573
nope, delaying between key presses doesnt do anything
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1000 total points
ID: 9860612
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
 
LVL 5

Expert Comment

by:Cimperiali
ID: 9861900
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 9862256
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
 
LVL 1

Author Comment

by:Moglor
ID: 9866111
thanks for the hard work guys
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9866153
Glad we can help.

Merry X'mas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question