C#: How do you display the modifier key name + non-modifier key name in this keydown event?

前端 未结 4 1122
攒了一身酷
攒了一身酷 2021-01-21 22:01

I am using this code to detect whether modifier keys are being held down in the KeyDown event of a text box.

    private void txtShortcut_KeyDown(object sender,          


        
4条回答
  •  长发绾君心
    2021-01-21 22:38

    You need to display the right string depending on the bool value. To do things like that in a single statement, use the ? : operator:

    txtShortcut.Text = (e.Shift ? "[Shift]" : string.Empty) + 
                       (e.Control ? "[Ctrl]" : string.Empty) + ...;
    

    Generally, it's:

     ?  : 
    

    It works with any type.

提交回复
热议问题