Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CHECK FORM KEY DOWN

Posted on 2013-06-18
14
Medium Priority
?
342 Views
Last Modified: 2013-06-19
Hi All,

I have form and textbox keydown event.

The form event is fired first then textbox.

I want to check at form event if the keydown is Key F, Crtl, Alt, Shift.
I want to exit from the sub as soon as possible, otherwise it will continue to check those keys at other sub.

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39258318
Hi

refer following code

 Dim nextSub As Boolean

    Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If nextSub = False Then
            Exit Sub
        End If

        Debug.Print("Textbox Event")
    End Sub

    Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
    End Sub

Open in new window


I have declared a variable nextSub at form level.

In form's KeyDown event, I check whether it is "F" with Shift key then I set value of nextSub to false.

And In KeyDown Event of Textbox, I check If nextSub is false, then exit from That Sub... Thus it will not execute the KeyDown of Textbox.


I think this will help you ...

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39258428
Hi Deepak Lakkad,

I try :

 Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Kalau bukan F, Ctrl dan Alt maka keluar

        If Not e.Shift Or _
            Not e.KeyCode = 70 Or _
            Not e.Alt Or _
            Not e.Control Or _
            Not e.KeyCode = Keys.Escape Then Exit Sub

        If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        End If

        Me.Check_Keys(sender, e)

    End Sub

Anything wrong  ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39258634
Hi,

In your code,

If Not e.Shift Or _
            Not e.KeyCode = 70 Or _
            Not e.Alt Or _
            Not e.Control Or _
            Not e.KeyCode = Keys.Escape Then Exit Sub

Open in new window


will terminate the execute of sub if escape is pressed.

While following code will execute only if Escape is pressed.

 
If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        End If

Open in new window


So, In this case, the second block will never execute ...

I think this is the problem in your code ...

- Deepak Lakkad
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:emi_sastra
ID: 39260118
Ok. I try :

Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Kalau bukan F, Ctrl dan Alt maka keluar

        If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        Else
            If Not e.Shift Or _
           Not e.KeyCode = 70 Or _
           Not e.Alt Or _
           Not e.Control Then Exit Sub
        End If

        Me.Check_Keys(sender, e)

    End Sub

When I press F keys, it goes to exit sub ?

What's wrong ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261264
Hi

You are making mistake in write following condition

  If Not e.Shift Or _
           Not e.KeyCode = 70 Or _
           Not e.Alt Or _
           Not e.Control Then Exit Sub
        End If

I don't understand what do you want to do. Can you please explain your requirement so I can write few lines of code for it.

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261290
-I don't understand what do you want to do. Can you please explain your requirement so I can write few lines of code for it.
At form keydown event if not special keys (Shift, Ctrl, Alt, F(x) and Esc) I want to exit from that sub.

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261316
Hi

For your requirement, If should be written as following


If e.KeyCode = Keys.F1 Or e.KeyCode = Keys.F2 Or e.KeyCode = Keys.F3 Or e.KeyCode = Keys.F4 Or e.KeyCode = Keys.F5 Or _
               e.KeyCode = Keys.F6 Or e.KeyCode = Keys.F7 Or e.KeyCode = Keys.F8 Or e.KeyCode = Keys.F9 Or e.KeyCode = Keys.F10 Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

                Exit Sub
            End If

Please check it

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261325
Ok. But I have to write down those F Keys ?

Below is your code :

 Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
    End Sub

What is e.KeyCode = 70 means ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261326
Hi


replace

 If e.KeyCode = 70 And e.Shift = True Then

with

newly given code

- or -

you can replace

If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
End if

with new code


- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261361
If e.KeyCode = Keys.F1 Or e.KeyCode = Keys.F2 Or e.KeyCode = Keys.F3 Or e.KeyCode = Keys.F4 Or e.KeyCode = Keys.F5 Or _
               e.KeyCode = Keys.F6 Or e.KeyCode = Keys.F7 Or e.KeyCode = Keys.F8 Or e.KeyCode = Keys.F9 Or e.KeyCode = Keys.F10 Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

                Exit Sub
            End If

Should it be :

IF NOT (KeyCode .. ) THEN EXIT SUB

Since this is at form event.

Is it correct ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261379
yes you are right ...
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261411
Is there any way to check just F key, does not have to write all that FKeys.

Thank you.
0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 2000 total points
ID: 39261455
Change the if condition as shown below:

If (e.KeyCode >= Keys.F1 And e.KeyCode <= Keys.F12) Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

If will work same way

- Deepak Lakkad
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39261462
Hi Deepak,

Thank you very much for your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question