• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7071
  • Last Modified:

Need to Capture CTRL + ENTER key press in VB.NET

Hello.

I need to capture a CTRL + ENTER key press in VB.NET. I only want this combination, for now other combinations can be ignored.

I have some code that captures an F-KEY press, but that didn't work for CTRL + ENTER. (see below)

e.KeyData.ToString() = "F12"  (This lets me know if the F12 key was pressed).

Thanks in advance

0
WaldaInc
Asked:
WaldaInc
  • 3
  • 2
1 Solution
 
sivachirravuriTeam Lead Commented:
Check following code for ENTER key press. Use same logic for CTRL also.

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
' The keypressed method uses the KeyChar property to check whether the ENTER key is pressed.
' If the ENTER key is pressed, the Handled property is set to true,  to indicate the event is handled.

If e.KeyChar = Microsoft.VisualBasic.ChrW(65) Then
e.Handled = True
MsgBox("The following key was depressed " + ChrW(65))
End If

End Sub

0
 
sivachirravuriTeam Lead Commented:
In addition to above, if you want to use KeyData & capture ENTER event.. Following link might be handy,

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.processdialogkey.aspx

Let me know if any problem.
0
 
WaldaIncAuthor Commented:
Sorry, but that first solution isn't working at all.  Pressing the enter key doesn't fire the messagebox.



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ToFroCommented:
This code will work, but you have to set the KeyPreview Property of the form to True:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Modifiers = Keys.Control Then
            If e.KeyCode = Keys.Enter Then
                MessageBox.Show("Ctrl + Enter")
            End If
        End If
End Sub
0
 
WaldaIncAuthor Commented:
That one did the trick.

Thanks ToFro.

0
 
sivachirravuriTeam Lead Commented:
Yeh KeyPreview Property of the form should be to True !! Kool.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now