looking for keyboard input...

Tinbeard

Member
Joined
May 30, 2005
Messages
16
Location
UK
Programming Experience
1-3
I'm writing a simple calculator project just to see if I can actually do it.
At the moment it all works ok but only via the buttons on the form (which replicate windows calculator).
What I want to do is add the funtionality to allow it to be operated from the keyboard, so I need to look for the following key presses and act accordingly (1,2,3,4,5,6,7,8,9,0,.,,-,=,*,+,/).
The question is how do I monitor keyboard input ?

Thanks in advance
 
PHP:
 Dim firstvalue As Integer 
 
Private Sub TextBox1_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
 
If e.KeyCode = Keys.Add Then
 
firstvalue = Integer.Parse(TextBox1.Text)
 
ElseIf e.KeyCode = Keys.Enter Then
 
Me.TextBox1.Text = Integer.Parse(TextBox1.Text) + firstvalue
 
EndIf
 
End Sub
 
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
 
If e.KeyCode = Keys.Add Then
 
Me.TextBox1.Text = String.Empty
 
End If
 
End Sub


try this i must say very rough example ... but trust me that's all i can do at the moment ... however it's only for "+" (add) and "enter" keys ... for other i guess you will find sense if you got that above.
And about numbers ... i guess you don't need to catch key events ... only operators.

Cheers ;)
 
actually you can catch numbers and operators in the form's key press event and the enter/escape key in the form's keydown &/or keyup event's (keydown is preferable)

so it'll even act like the windows calculator and everything
 
You never know what will be in focus at the time the user decides to use the keys. I'm still new to .NET but couldn't you handle the keydown events of both the form and textbox in the same routine? (Using "Handles")

Might be wise to not use a textbox at all. You might use a white label with a (fixed) border around it. Open Windows calculator - seems like that's how they did it.
 
If the form's KeyPreview property is set to True then it will get all keyboard events regardless of what's focused.

One thing that bugs me about the Windows Calculator is that you can't focus the TextBox (which it almost certainly is, given that its cursor is IBeam) containing the value to copy it. I would suggest using a TextBox with its ReadOnly property set to True. The only drawback is that doing this automatically sets the BackColor to SystemColors.Control upon initialisation. If you don't want that, you need to set the BackColor back to SystemColors.Window after initialisation, probably in the form's Load event handler.
 
Thanks for the help on this, I've changed the textbox ReadOnly proprerty to true and I'm going to try and use the forms KeyPreview property to capture and handle the keyboard input.
 
LHandy said:
I've always known about KeyPreview and what it does but I always forget about it. Damn it I suck.

it's not a matter of whether you suck or not, it's simply a matter of knowing the properties and capabilities of the object's and being aware of how you can use them together and whatnot

the more form versetile projects you work on, the more familiar you'll become with the form's properties

it's a matter of perspective really
 
I'm getting pretty close to completeing the simple calculator I'm trying to code, at the moment it works fine just using the keyboard, and it works fine just using the buttons ...

There is however something strange happening when you use a combination of buttons and keys which I just can't understand.

If i click on '3' then on '+' then on '3' (any numbers will produce the bug) then 'press the enter key" it just adds the last digit entered (in this case' 3') to the current label display.

I can't for the life of me figure this out, would someone mind looking through the project and let me know what's causing this :confused:

I'm still very new to VB.Net so any other advice regarding the method of coding I've used would also be greatfully received.

http://www.marcb.pwp.blueyonder.co.uk/Calculator2.zip
 
You can have the KeyDown even of the form call the .click event of the appropriate button. This isn't the code you would use, necessarily, but it will show you what I mean:

VB.NET:
Select Case pressedKey
  Case "3"
	  Call cmd3_Click()
  Case vbKeyEnter
	  Call cmdEquals_Click()
End Select

Not sure if vbKeyEnter is a real key constant or not. But I think you get my drift.

Edit: I didn't look at your code. But if you're not doing this, this should remedy it as pressing the keys will pass it off as if the button was clicked. So all should be the same.
 
The root of the problem came to me whilst I was lying in bed lastnight :D

I can't believe I missed this.:eek:

When I use the buttons to enter the calculation e.g '3' + '3', the last button I clicked is still selected, so when I press enter the button is effectively being clicked again, so the last digit will be added to the display.
What I need to do is add some code that will deselect the buttons once the click event has been completed.

I've added the following to the end of every button click event and it's solved the problem
VB.NET:
 lblDisplay.Focus()
 
LHandy said:
You can have the KeyDown even of the form call the .click event of the appropriate button. This isn't the code you would use, necessarily, but it will show you what I mean:

VB.NET:
Select Case pressedKey
  Case "3"
	  Call cmd3_Click()
  Case vbKeyEnter
	  Call cmdEquals_Click()
End Select

Not sure if vbKeyEnter is a real key constant or not. But I think you get my drift.

Edit: I didn't look at your code. But if you're not doing this, this should remedy it as pressing the keys will pass it off as if the button was clicked. So all should be the same.
VB.NET:
'in the keydown event:
Select Case e.KeyCode
  Case Keys.3
	btn3.PerformClick()
  Case Keys.Enter
	btnEquals.PerformClick()
End Select
 
Back
Top