Multiple Keyboard Shortcuts

人盡茶涼 提交于 2019-12-11 20:28:14

问题


I am using the following code to try and get Ctrl+S to press a toolstrip button:

 Private Sub take_register_KeyDown(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyCode = Keys.S And Keys.Control Then
        ToolStripButton20.PerformClick()

    End If

End Sub

I am a newbie at this, so I dont understand millions of lines of coding, so can you please keep it as simple as possible :-) .


回答1:


Total guesswork here since there is no actual question. First, in order to get something like that work, you need to set KeyPreview = True for the form. Next, you probably want to use the KeyDown event instead of KeyPress:

Private Sub Form1_KeyDown(...)
    ' when possible use AndAlso for speed and to avoid some errors in
    ' some situations.  if e.Control is False, the second part wont be evaluated.
    If e.Control AndAlso e.KeyCode = Keys.S Then

        ToolStripButton20.PerformClick()
    End If
End Sub

To repeat: you can simply assign a shortcut key combo to the menu object in the designer and let .NET do all the work. ...and I don't know where "multiple" comes in to play unless Ctrl+S counts as multiple somehow.



来源:https://stackoverflow.com/questions/19366782/multiple-keyboard-shortcuts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!