.net difference between right shift and left shift keys

后端 未结 4 1250
陌清茗
陌清茗 2020-12-19 06:46

I am currently working on an application which requires different behaviour based on whether the user presses the right or left shift key (RShiftKey, LShiftKey), however whe

4条回答
  •  离开以前
    2020-12-19 07:34

    I don't know if this post will help you or not, but it looks like you may have to mess with InteropServices and Diagnostics:

    MSDN Forum: How to send Left/Right shift key

    Edit: I finally figured out how to make GetAsyncKeyState() work, as adrianbanks revealed.

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern short GetAsyncKeyState(Keys vKey);
    
    private void theForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.ShiftKey)
        {
            if (Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)))
            {
                Console.WriteLine("Left");
            }
            if (Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)))
            {
                Console.WriteLine("Right");
            }
        }
    }
    

提交回复
热议问题