Avatar of glwhatrup
 asked on

"Alt" & "Tab" keys in VB

Hi guys,

I have the following code which is the equivalent of pressing "windows" & "F" to bring up the search bar. (courtesy of mvidas - thanks!)

If I have this in a live powerpoint slideshow, it brings the search bar up behind the active slideshow, so I would like the modify the code to add "Alt"&"Tab" to bring it to the front.

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

Sub OpenSearchWindow()
 keybd_event &H5B, 0, 0, 0 'windows key
 keybd_event &H46, 0, 0, 0 'F
 keybd_event &H5B, 0, &H2, 0 'keys up
End Sub

Obviously it would be "keybd_event...", but I'm not sure what the codes for the rest of the keyboard are.

Any help greatly appreciated


Visual Basic Classic

Avatar of undefined
Last Comment

8/22/2022 - Mon

Cem Türk

Cem Türk

vinnyd79 you are fast;)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

This is the 'MapVirtualKey' method to send an "Alt Tab" combination :

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 Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Const VK_TAB = &H9 'Tab
Const VK_MENU = &H12 'ALT

    keybd_event VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0  ' 0=press alt
    keybd_event VK_TAB, MapVirtualKey(VK_TAB, 0), 0, 0
    keybd_event VK_TAB, MapVirtualKey(VK_TAB, 0), 2, 0
    keybd_event VK_MENU, MapVirtualKey(VK_MENU, 0), 2, 0  ' 2=release alt


Dear All,

Thanks for all your quick replies and useful links. I accepted vinnyd79's answer, as it just added "alt"&"tab" to the code I already had - which was all I was after.

vb_elmars solution is a valid one as well though - so thankyou to all!