Examples for Visual Basic for Application accessing the Windows API: VkKeyScanEx
VkKeyScanEx can be used to determine which keys a user has to type on his keyboard so that it results in a desired character. This is especially useful for characters that are obtained by a combination of the shift and another key.
This example needs the VBA declarations of the Windows API which can be found here.
option explicit
dim keyboardLayout as long
sub main() ' {
keyboardLayout = GetKeyboardLayout(0)
showCharInfo "c"
showCharInfo "C"
showCharInfo "="
showCharInfo "+"
' showCharInfo chr(1)
end sub ' }
sub showCharInfo(char as string) ' {
dim keyScan as integer
dim shift as boolean
dim ctrl as boolean
dim vkKey as integer
keyScan = VkKeyScanEx(asc(char), keyboardLayout)
shift = keyScan and &h100
ctrl = keyScan and &h200
vkKey = keyScan and &h0ff
debug.print char & ": " + iif(shift, "shift ", " ") + iif(ctrl, "ctrl ", " ") & chr(vkKey) & " " & vkKey
end sub ' }