473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Keystrokes..............

Hello. I have managed to consume the key 9 when it is pressed,
by using the following code:

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress

'If key 9 on keyboard is pressed show this in a messagebox
'and then consume the stroke using e.handled=true

If e.KeyChar = ChrW(57) Then
MessageBox.Show("hello", "57", MessageBoxButtons.OK,
MessageBoxIcon.Information)
e.Handled = True
End If
End Sub

However the above code will not consume the use of the "Backspace" key. That
code looks like the following:

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress

'If Backspace key on keyboard is pressed show this in a messagebox
'and then consume the stroke using e.handled=true

If e.KeyChar = ChrW(8) Then
MessageBox.Show("hello", "8 Backspace", MessageBoxButtons.OK,
MessageBoxIcon.Information)
e.Handled = True
End If
End Sub

Anyone happen to know why??????

Thank you,

Brenton Garman

Apr 4 '06 #1
4 1319
Hello Brenton,

Backspace is Keys.Back

if (e.KeyCode == Keys.Back)
{
....
}

BG> Hello. I have managed to consume the key 9 when it is pressed, by
BG> using the following code:
BG>
BG> Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
BG> System.Windows.Forms.KeyPressEventArgs) Handles
BG> RichTextBox1.KeyPress
BG>
BG> 'If key 9 on keyboard is pressed show this in a messagebox
BG> 'and then consume the stroke using e.handled=true
BG> If e.KeyChar = ChrW(57) Then
BG> MessageBox.Show("hello", "57", MessageBoxButtons.OK,
BG> MessageBoxIcon.Information)
BG> e.Handled = True
BG> End If
BG> End Sub
BG> However the above code will not consume the use of the "Backspace"
BG> key. That code looks like the following:
BG>
BG> Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As
BG> System.Windows.Forms.KeyPressEventArgs) Handles
BG> RichTextBox1.KeyPress
BG>
BG> 'If Backspace key on keyboard is pressed show this in a
BG> messagebox
BG> 'and then consume the stroke using e.handled=true
BG> If e.KeyChar = ChrW(8) Then
BG> MessageBox.Show("hello", "8 Backspace",
BG> MessageBoxButtons.OK,
BG> MessageBoxIcon.Information)
BG> e.Handled = True
BG> End If
BG> End Sub
BG> Anyone happen to know why??????
BG>
BG> Thank you,
BG>
BG> Brenton Garman
BG>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Apr 4 '06 #2
> However the above code will not consume the use of the "Backspace" key. That
code looks like the following:


I think you need to handle KeyDown rather than KeyPress to consume some of
the keys. I sympathize as the documentation is very fuzzy about keystroke
handling. What works for me in most settings is a form with KeyPreview true
and this event handler:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles MyBase.KeyDown
' KeyDown event handling goes here
End Sub

Apr 4 '06 #3
Thank you Gentlemen Both of you! AMercer I used your code and suggestion and
part
of Mr.Nemstev's code and got the app to work. The following code will
consume both
Del and Bs keys, but allows all numeric and letters to be used.

Private Sub frmGuitarWorks_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown

e.Handled = True

End Sub

That is it! Awesome! Thank you Both! Now I can further tailor the code to my
needs!
Very Grateful! Oh, ya, also set the form's KeyPreview Property to true.

"AMercer" wrote:
However the above code will not consume the use of the "Backspace" key. That
code looks like the following:


I think you need to handle KeyDown rather than KeyPress to consume some of
the keys. I sympathize as the documentation is very fuzzy about keystroke
handling. What works for me in most settings is a form with KeyPreview true
and this event handler:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _
Handles MyBase.KeyDown
' KeyDown event handling goes here
End Sub

Apr 4 '06 #4
Hi,

AMercer is right.

The KeyPress event cannot be used to trap values that cannot be seen in
a TextBox, for instance. These and all other keys such as the Control
keys, arrow keys, Function keys, backspace etc. will only trigger the
KeyDown event, not KeyPress.

So, you might consider coding only the KeyDown event in your
application.

Regards,

Cerebrus.

Apr 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olli Piepponen | last post by:
Hi, I'm having a little problem catching keystrokes under Windows. I did a little research and found that with mscvrt.getch() one can cath a single key that is pressed. However this doesn't work...
3
by: Wayne | last post by:
I have a console application (ssh2.exe) that requires keystrokes to be sent to it after activating. I have worked that it needs to be started in it own thread, but capturing the process and...
5
by: Lucas Tam | last post by:
Hi all, Has anyone done this before? Send client side keystrokes to an application from a webpapge? This maybe more of a Javascript question. In short, I have an application on the client's...
2
by: zak | last post by:
I have some testing to do and I need to put a whole set of data in I want to Record and replay in a loop keystrokes. An idea
3
by: Gunnar Syren | last post by:
I'm trying to implement a macro feature in my application by recording and playing back keystrokes. At first I thought it would be enough to catch KeyDown in my main form, but I soon realized that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.