block keys

frix199

Member
Joined
Jul 18, 2005
Messages
19
Programming Experience
1-3
hello...

how can some1 block the key "alt" on a program??

i know how to block other keys like;

VB.NET:
	Private Sub Form1_KeyPress(...) Handles MyBase.KeyPress
		If e.KeyChar = "k" Then
			e.Handled = True
		End If
	End Sub

but how can i block keys like alt or delete???
 
Yes like he said ... find ascii code for particular letter and just handle ... in your case:

PHP:
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) HandlesMyBase.KeyPress
  
If e.KeyChar = Chr(75) Or e.KeyChar = Chr(107) Then'k and K are not allowed
 
e.Handled = True
 
End If
 
End Sub
But this will not work however unless you set up KeyPreview (go in designer and from properties window set KeyPreview property to TRUE).

Btw, you can use KeyDown or KeyUp if you haven't used already.
i.e.
PHP:
 {...} 
If e.KeyCode = Keys.K Then

e.Handled = True

ElseIf e.KeyValue = Keys.J Then

'do something

End If
{...}


Regards ;)
 
next time do this ... add a textbox to the form and join this code to that
VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TextBox1_KeyDown([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.KeyEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TextBox1.KeyDown
 
TextBox1.Text = e.KeyValue 'will return ascii code for each pressed key
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
[/color][/size]

:D btw, alt is 18

Regards ;)
 
Sigh.... the KEYCODES and ASCII CODES are TWO DIFFERENT things.... one represents the character (ASCII) and the other represents the keypressed (keycode). Please don't confuse the two. They are completely different from each other and serve two different purposes.

The Alt key does not represent any character. Ever. Therefore, it would be IMPOSSIBLE to get the ASCII code for the Alt key. This is why frix199 couldn't find it. It doesn't exist.

It does however have a keycode. This is why Kulrom's code: e.KeyValue will work in this case.

All keys on a keyboard have a keycode, the F keys, the enter, the space, the letters, etc. But only those that actualy produce characters will be able to get an ASCII value from.

-tg
 
Back
Top