Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,303 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 783 people online right now. Registration is fast and FREE... Join Now!




Disable Escape Key

 
Reply to this topicStart new topic

Disable Escape Key

hemo
6 Jan, 2009 - 02:17 PM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2007
Posts: 34



Thanked: 1 times
My Contributions
I need to disable the escape key on loading my program and enable on closing.
Although I have searched vb 2008 books and forums I can’t find any reference.
Can anyone Help.

User is offlineProfile CardPM
+Quote Post


modi123_1
RE: Disable Escape Key
6 Jan, 2009 - 02:50 PM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,216



Thanked: 49 times
Dream Kudos: 150
My Contributions
Just catch the key press in your program and set the e.handled = true.... you don't really disable it for the whole OS...
User is offlineProfile CardPM
+Quote Post

Core
RE: Disable Escape Key
6 Jan, 2009 - 03:45 PM
Post #3

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
If you want to disable the escape key completely, that will involve some API calls. You could look at this on MSDN.
User is offlineProfile CardPM
+Quote Post

hemo
RE: Disable Escape Key
7 Jan, 2009 - 03:12 AM
Post #4

New D.I.C Head
*

Joined: 2 Jan, 2007
Posts: 34



Thanked: 1 times
My Contributions
QUOTE(modi123_1 @ 6 Jan, 2009 - 02:50 PM) *

Just catch the key press in your program and set the e.handled = true.... you don't really disable it for the whole OS...


Thanks for the info, I only need to disable the esc key for the initial start up screen, I had previously tried the following without success.
CODE

Private Sub Form1_KeyPress(ByVal as Object, ByVal e as Systems.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If CBool(Keys.Escape) = True Then
e.Handled = True
End If
End Sub


Am I missing the obvious? Any help would be appreciated.
hemo

This post has been edited by Jayman: 7 Jan, 2009 - 08:26 AM
User is offlineProfile CardPM
+Quote Post

modi123_1
RE: Disable Escape Key
7 Jan, 2009 - 07:42 AM
Post #5

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,216



Thanked: 49 times
Dream Kudos: 150
My Contributions
I'm sure you know when you want this escape kep to be not valid and when you want to be valid. .so throw in t he flag flips before and after you need it.

CODE

'-- declare variable.
private _bDisableEscape as bool = false

'-- lines of code that call your initial start up of the screen
_bDisableEscape  = true

'-- lines of code that declare you are done with the initial start up of your screen
_bDisableEscape = false

    Private Sub Form_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        if _bDisableEscape then If e.KeyCode = Windows.Forms.Keys.Escape Then e.Handled = True
    End Sub

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Disable Escape Key
7 Jan, 2009 - 08:30 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You need to compare the Key to the KeyCode. I am not sure why you are trying to convert the Key to a Boolean.

It should also be noted that the Form must have focus in order to capture the keypress. If another control on your form has focus then this event will not execute, since it is specific to the form.

CODE

Private Sub Form1_KeyPress(ByVal as Object, ByVal e as Systems.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
   If e.KeyCode = Keys.Escape Then
      e.Handled = True
   End If
End Sub

User is offlineProfile CardPM
+Quote Post

hemo
RE: Disable Escape Key
7 Jan, 2009 - 01:25 PM
Post #7

New D.I.C Head
*

Joined: 2 Jan, 2007
Posts: 34



Thanked: 1 times
My Contributions
QUOTE(Jayman @ 7 Jan, 2009 - 08:30 AM) *

You need to compare the Key to the KeyCode. I am not sure why you are trying to convert the Key to a Boolean.

It should also be noted that the Form must have focus in order to capture the keypress. If another control on your form has focus then this event will not execute, since it is specific to the form.

CODE

Private Sub Form1_KeyPress(ByVal as Object, ByVal e as Systems.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
   If e.KeyCode = Keys.Escape Then
      e.Handled = True
   End If
End Sub



Thanks both for the code. Unfortunately the Escape key remains live. Ideally I what to run the code in the form Load event. With Jaman’s Code e.KeyCode returns an error [not a member of KeyPressEventArgs]

Perhaps it’s worth stating the reason; I have a start up form which has been customised with a Log In textbox password which is set in the start up form properties. To achieve a smooth display of the main form I have called it in the Start Up form load event, which works ok.
Except that if Alt+Esc is press the start up form is closed and the main form display, thereby bypassing the password. I have tried displaying modally without success.

hemo

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Disable Escape Key
7 Jan, 2009 - 02:55 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
My mistake, I was using the code for the KeyDown or KeyUp event.

For the KeyPress it should be:
CODE

Private Sub Form1_KeyPress(ByVal as Object, ByVal e as Systems.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
   If e.KeyCode = ChrW(Keys.Escape) Then
      e.Handled = True
   End If
End Sub

User is offlineProfile CardPM
+Quote Post

hemo
RE: Disable Escape Key
7 Jan, 2009 - 03:29 PM
Post #9

New D.I.C Head
*

Joined: 2 Jan, 2007
Posts: 34



Thanked: 1 times
My Contributions
QUOTE(Jayman @ 7 Jan, 2009 - 02:55 PM) *

My mistake, I was using the code for the KeyDown or KeyUp event.

For the KeyPress it should be:
CODE

Private Sub Form1_KeyPress(ByVal as Object, ByVal e as Systems.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
   If e.KeyCode = ChrW(Keys.Escape) Then
      e.Handled = True
   End If
End Sub





'e.KeyCode' still returns an error [not a member of KeyPressEventArgs]

hemo
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Disable Escape Key
7 Jan, 2009 - 05:50 PM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Post the code that you currently have, it sounds like you have the code in the wrong event.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:05PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month