How send Lwin shortcut key in vb.net

前端 未结 3 1745
北荒
北荒 2020-12-07 06:15

Can someone tell mew how to send Shortcut keys in vb.net? The shortcut keys are {LEFTWIN} + {ADD} amd {LEFTWIN} + {SUBTRACT}. Tried SendKeys.Send it does not work.

相关标签:
3条回答
  • 2020-12-07 06:58

    You said you tried

     SendKeys.Sned("KEY")
    

    The correct is

     SendKeys.Send("KEY")
    

    Besides that, what key you are referring with "Leftwin" ?

    Edited:

    Sorry for that, I never used Sendkeys, and I knew "LWIN" by Winkey...

    Btw, Try using

    SendKeys.Send(Keys.LWin)
    

    SendKeys send string, so this SendKeys.Send(Keys.LWin) actually should send the code of Keys.LWin

    0 讨论(0)
  • 2020-12-07 07:00

    Finally this worked for me :)

    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As IntPtr, ByVal bScan As IntPtr, ByVal dwFlags As IntPtr, ByVal dwExtraInfo As IntPtr)
    Private Const VK_STARTKEY = &H5B
    Private Const VK_SUBTRACT = &H6D
    Private Const VK_ADD = &H6B
    Private Const VK_ESCAPE = &H1B
    Private Const KEYEVENTF_KEYUP = &H2
    Private Const KEYEVENTF_KEYDOWN = &H
    
    Call keybd_event(VK_STARTKEY, 0, KEYEVENTF_KEYDOWN, 0)
    Call keybd_event(VK_ADD, 0, KEYEVENTF_KEYDOWN, 0)
    Call keybd_event(VK_ADD, 0, KEYEVENTF_KEYUP, 0)
    Call keybd_event(VK_STARTKEY, 0, KEYEVENTF_KEYUP, 0)
    

    Key codes: http://vbcity.com/cfs-filesystemfile.ashx/__key/CommunityServer.Components.PostAttachments/00.00.11.85.52/Api.txt

    EDIT The keybd_event is deprecated so I am moving to SendInput(). The first solution works best for me :-)

    0 讨论(0)
  • 2020-12-07 07:01

    Sending LWin is not possible through SendKeys.Send(). In order to do so you have to P/Invoke the WinAPI's SendInput() function.

    Here's a wrapper class I've created for that purpose:

    EDIT (2019-09-20)

    InputHelper has since long been moved to its own library. The answer has been updated to reflect this change.

    Download InputHelper from GitHub:
    https://github.com/Visual-Vincent/InputHelper/releases

    Here's how you'd use it:

    InputHelper.Keyboard.SetKeyState(Keys.LWin, True) 'Hold LWin.
    InputHelper.Keyboard.PressKey(Keys.Add) 'Press the ADD key.
    InputHelper.Keyboard.SetKeyState(Keys.LWin, False) 'Release LWin.
    
    0 讨论(0)
提交回复
热议问题