Capture Key Sequence via ProcessCmdKey

前端 未结 1 1458
萌比男神i
萌比男神i 2020-12-22 01:29

I override ProcessCmdKey in my application and can get any single keypress with modifiers (eg. Alt+Ctrl+X). What I want to do is mimic the short cut handling of say ReSharp

1条回答
  •  一向
    一向 (楼主)
    2020-12-22 01:34

    In order to achieve the functionality you want you simply need to keep track of the sequence of KeyPress events.

    You can create a class to keep track of the last key combination that was pressed in ProcessCmdKey. If that particular combination does not match a mapped command but it is the first element of a sequence you can store it in your class. Then the next time ProcessCmdKey is activated check your new KeyPressTracker class to determine if a sequence has been started. If it has then check if the newly pressed key combination is the second element of one you specify. Please see the pseudocode example below:

    Step 1: ProcessCmdKey is activated. The key combination is Ctrl+R, this does not match a command that you want to process but it is the first element of a sequence that you want to use (Ctrl+R+M).

    Step 2: Store this key-press in a new class you created to keep track of the last key-press.

    KeyPressTracker.Store(KeyCode, Modifiers);
    

    Step 3: ProcessCmdKey is activated a second time. This time, the key combination is Ctrl+M which is not a key-press we're looking for but is the second element of a sequence. We check the last stored keypress using the new KeyPressTracker class. This will allow you to match a "sequence" such as Ctrl+R and Ctrl+M.

    var lastKeyPress = KeyPressTracker.GetLastKeyPress();
    
    if (lastKeyPress == "Ctrl+R" && currentKeyPress == "Ctrl+M")
    {   
        // Show Refactor dialog
    }
    

    0 讨论(0)
提交回复
热议问题