以编程方式按“右移”键

Posted

技术标签:

【中文标题】以编程方式按“右移”键【英文标题】:Programmatically press "Right Shift" Key 【发布时间】:2022-01-10 23:34:05 【问题描述】:

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

我拥有的是:

SendKeys.Send("RSHIFT")

我知道这种转变是这样的:

SendKeys.Send("+")

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

有人可以帮我处理这段代码吗?

【问题讨论】:

我在SendKeys.Send 识别的特殊键之间看不到任何 RSHIFT,从我在发送中看到的内容来看,无法从左 shift 键识别右侧 是的!这就是我发布这个问题的原因,以了解如何完成任务 也许有不同的方式来完成你的任务。为什么需要发送右移键?您是否正在尝试与其他应用程序通信? 是的,它需要专门的右 Shift 键 我已经更新了我之前的答案,看看新的。 【参考方案1】:

Found this after some creative keyword combinations

发送密钥代码就是这样:

Keys key = Keys.RShiftKey;//Right shift key  
SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_KEYDOWN, (int)key, 1);

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

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

【讨论】:

【参考方案2】:

使用不需要窗口句柄的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

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();
        
    

用法:

MyKeyPress.Send(Keys.LShiftKey)

【讨论】:

以上是关于以编程方式按“右移”键的主要内容,如果未能解决你的问题,请参考以下文章

奇怪的 UITable 分隔符问题 - 右移

python怎么集体右移

使用浮点运算对整数数据进行右移运算?

Linux主要shell命令详解(下)

Excel按回车键时光标右移而不是下移,怎么回事呢?

Javascript 右移0位的作用