Question:
Arrow keys won't work in visual basic.?
anonymous
2010-03-16 16:22:42 UTC
I'm trying to make a game in visual basic, but I have a problem. I'm using the KeyDown event to move a button (Just for practice, I'll eventually replace it with a picture box). Here is the code if you want it--

Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
If e.KeyCode = Keys.Right Then
Button1.Location = New Point(Button1.Location.X + 2, Button1.Location.Y)
End If
End Sub

The arrow keys don't seem to work nor does the TAB key. All the letter keys and number keys seem to work, so I don't get what my problem is. I'm using a laptop if that matters.
Please tell me what I did wrong or if VB somehow just isn't agreeing with me please try and provide a solution.
Three answers:
Tharaka Deshan
2010-03-17 02:57:59 UTC
Actually some keys not work if form containing any objects like text box ,buttons ...etc.



The reason is some keys having advance funtionalities with the form and system give a priority to them.

Ex: Assume a form with button.If you hit the space button in the keyboard it taken as you click the button,because from the space you can click a button at a form.So your code will not work.



So best thing to do in your project is do not use buttons in the form.

Then write to Form_KeyDown() event.

It will work.



Good luck!
Shadow Wolf
2010-03-16 17:44:38 UTC
Rather than worry about the key not working, why not write a routine to see what is actually happening?



When you press a key, what do you get? This is the question you need to answer.



When you do this, in nearly all programming languages, you end up with what are called extended keys for the special use keys except for the tab key which is actually ASCII code 9 (HT or horizontal tab) and a few others.



With a laptop, you may have an additional complication of having keys that change definitions when extra shift or function keys are pressed. Is it confusing yet? Any wonder that you aren't getting what you expected?



So rather than trying to give you code, I think the best route is to try and see what keys your computer thinks is being pressed rather than what you or I think is being pressed. It shouldn't take much to write a little program that will display the key codes when you press them.



If you do this, you'll gain a better understanding of how your computer handles key presses. Besides, a little program to report what key is pressed will also be good practice. Once you see what is happening, you can use the correct code or write code to work around the problem.



Shadow Wolf
Judith
2016-04-15 07:06:41 UTC
what code of left and right arrows in scientific caculator in visual basic 6.0


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...