Multiple Key Events, like Ctrl+S

Themanhunt 2 Tallied Votes 838 Views Share

Lets say that you are working on a program such like Notepad, for instance. You want to make a Ctrl+S key event to save your work.

This is the ONLY way I have found out that does not cause any conflictions at all.

In the KeyUp Sub of, leys say, Form1 then this is how it should look for Ctrl+S

If e.Control = True And e.KeyCode = Keys.S Then
EXCECUTION CODE HERE
End If

And there you have it. Simple, short and effective. Perfect. You can minipulate this chunk of code with any keys that you want.

Enjoy messing around with it.

zinnqu 7 Junior Poster in Training

Here is a example

Public Class KeyCombos
 ''' <summary>
 ''' Stores the currently pressed keys.
 ''' </summary>
 Protected mKeys As New HashSet(Of Keys)
 ''' <summary>
 ''' Stores the KeyCombos along with an action to do when pressed.
 ''' </summary>
 ''' <remarks></remarks>
 Protected mKeyCombo As New Dictionary(Of Keys(), Action)
 Protected mCheckCombos As Boolean

 Public Sub New()
  mCheckCombos = True
 End Sub

 ''' <summary>
 ''' Enable the check of KeyCombos
 ''' </summary>
 Public Property CheckCombos() As Boolean
  Get
   Return mCheckCombos
  End Get
  Set(ByVal value As Boolean)
   mCheckCombos = value
  End Set
 End Property

 ''' <summary>
 ''' Add a new KeyCombo and Action.
 ''' </summary>
 ''' <param name="DoThis">Do what on keyCombo? (Note: Typically an AddressOf Sub)</param>
 ''' <param name="OnThisKeyCombo">The key combo.</param>
 ''' <remarks></remarks>
 Public Sub AddCombo(ByVal DoThis As Action, ByVal ParamArray OnThisKeyCombo() As Keys)
  If mKeyCombo.ContainsKey(OnThisKeyCombo) = False Then
   mKeyCombo.Add(OnThisKeyCombo, DoThis)
  End If
 End Sub

 ''' <summary>
 ''' Remove the keyCombo (along with the action)
 ''' </summary>
 ''' <param name="ThisKeyCombo">The KeyCombo to remove</param>
 ''' <remarks></remarks>
 Public Sub RemoveCombo(ByVal ParamArray ThisKeyCombo() As Keys)
  If mKeyCombo.ContainsKey(ThisKeyCombo) = False Then
   mKeyCombo.Remove(ThisKeyCombo)
  End If
 End Sub
 Public Sub KeyUP(ByVal k As Keys)
  mKeys.Remove(k)
 End Sub

 ''' <summary>
 ''' Check to see if the Key Combo is being done. 
 ''' </summary>
 ''' <param name="ThisKeyCombo"></param>
 ''' <returns>True if it is.</returns>
 ''' <remarks></remarks>
 Public Function KeyCombination(ByVal ParamArray ThisKeyCombo() As Keys) As Boolean
  Dim rb As Boolean = False
  For Each k In ThisKeyCombo
   rb = mKeys.Contains(k)
   If rb = False Then Exit For
  Next
  Return rb
 End Function

 Public Sub KeyDown(ByVal k As Keys)
  mKeys.Add(k)
  If mCheckCombos Then DoCombosCheck()
 End Sub
 Private Sub DoCombosCheck()
  For Each kc In mKeyCombo
   If KeyCombination(kc.Key) Then kc.Value.Invoke()
  Next
 End Sub

End Class


'
' Example
'
'Public Class Form1
' Dim a As New KeyCombos
'
' Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'  a.KeyDown(e.KeyCode)
' End Sub
'
' Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
'  a.KeyUP(e.KeyCode)
' End Sub
'
' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'  a.AddCombo(AddressOf f, Keys.A, Keys.B, Keys.C)
' End Sub
'
' Public Sub f()
'  Beep()
' End Sub
'End Class
Themanhunt 0 Newbie Poster

Well mines shorter and easier to understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.