以编程方式按“右 Shift”键

Edu*_*rds 6 c# vb.net keyboard

我无法找到一种以编程方式专门按下右 Shift 键的方法。我需要一个向下和向上的键(按下/释放)。

我所拥有的是:

SendKeys.Send("{RSHIFT}")
Run Code Online (Sandbox Code Playgroud)

我知道这种转变就像:

SendKeys.Send("+")
Run Code Online (Sandbox Code Playgroud)

我想这只是 Shift 键,但我特别需要一个右 Shift 键。

有人可以帮我解决这个代码吗?

Ali*_*eza 6

使用 keybd_event 你不需要窗口句柄

VB:

Public Class MyKeyPress
    <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
    Public Shared Sub keybd_event(ByVal bVk As UInteger, ByVal bScan As UInteger, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
    End Sub


    ' To find other keycodes check bellow link
    ' http://www.kbdedit.com/manual/low_level_vk_list.html
    Public Shared Sub Send(key As Keys)
        Select Case key
            Case Keys.A
                keybd_event(&H41, 0, 0, 0)
            Case Keys.Left
                keybd_event(&H25, 0, 0, 0)
            Case Keys.LShiftKey
                keybd_event(&HA0, 0, 0, 0)
            Case Keys.RShiftKey
                keybd_event(&HA1, 0, 0, 0)
            Case Else
                Throw New NotImplementedException()
        End Select
    End Sub
End Class

Run Code Online (Sandbox Code Playgroud)

C#:

public static class MyKeyPress
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);


    // To get other key codes check bellow link
    // http://www.kbdedit.com/manual/low_level_vk_list.html
    public static void Send(Keys key)
    {
        switch (key)
        {
            case Keys.A:
                keybd_event(0x41, 0, 0, 0);
                break;
            case Keys.Left:
                keybd_event(0x25, 0, 0, 0);
                break;
            case Keys.LShiftKey:
                keybd_event(0xA0, 0, 0, 0);
                break;
            case Keys.RShiftKey:
                keybd_event(0xA1, 0, 0, 0);
                break;
            default: throw new NotImplementedException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

MyKeyPress.Send(Keys.LShiftKey)
Run Code Online (Sandbox Code Playgroud)


小智 5

经过一些创意关键词组合后发现了这个

它构建为发送键码:

Keys key = Keys.RShiftKey;//Right shift key  
SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_KEYDOWN, (int)key, 1);
Run Code Online (Sandbox Code Playgroud)

我不知道这里的用例是什么,但请注意传递的窗口句柄参数:Process.GetCurrentProcess().MainWindowHandle

这会将击键发送给自身。如果您尝试将其发送到另一个进程/程序,您将需要传递该程序的窗口句柄。