Results 1 to 27 of 27

Thread: Problems with arrowkeys

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Problems with arrowkeys

    Hi,
    I'm using the arrowkeys to give steering commands to a moving object. I'm using the GetAsyncKeyState API. These commands are working well.
    But the commands do have a "nasty" side-effect, while using an arrow key the focus for the other control on the FORM is changing along ther direction of the arrowkey. If only the focus would be changing, the problem would be negletable, but when moving along an Option-Control the selected Option is changed, THAT is "nasty". Does anyone know a way around that. By the way, the quick solution to use other keys for the steering, is not really what I'm looking for.
    Thanks
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Junior Member
    Join Date
    Aug 2001
    Location
    Puerto Rico
    Posts
    19
    I ve been checking the api and it seems to me you have 2 options.

    1) Make an Keyboard Hook to intercept specific keys (in this case the arrow keys) and delete them and then do youu own moving routine.. Well that is some pretty dificult stuff...=)

    2) I Suggest you use this Code, and call ResetLastFocus every time your GetAsyncKeyState Api monitors an arrow key

    Option Explicit

    Dim LastInputControl as Variant

    Private Sub Timer1_Timer()
    Set LastInputControl=Me.ActiveControl
    end sub

    Private ResetLastFocus()
    LastInputControl=Control.SetFocus
    end sub

    I havent Debuged this much but it Should Work... Please let me now how it goes

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Thank's Thanathos
    I will check your code, but the answer will have to wait 'til next week, I'm on my way home. (TGIF ;-) ) I'll check it on Monday.
    Greetings to the Caribic
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Hi Thanathos;
    just tried your solution
    I had to modify it a bit, anyhow it doesn't work. I haven't found the solution to reset the focus to the LastInputControl (I used just a fixed control) after the arrowkey is pressed.
    Code:
    Private Sub Timer2_Timer()
    LastInputControl = Me.ActiveControl.Caption
    If (GetAsyncKeyState(vbKeyLeft)) Then
        ' turn left
        TurnIndicator.Value = TurnIndicator.Value - 1
    ElseIf (GetAsyncKeyState(vbKeyRight)) Then
        ' turn right
        TurnIndicator.Value = TurnIndicator.Value + 1
    End If
    If (GetAsyncKeyState(vbKeyUp)) And SpeedIndicator.Value > 0 Then
        ' Speed up
        SpeedIndicator.Value = SpeedIndicator.Value - 2
    ElseIf (GetAsyncKeyState(vbKeyDown)) And SpeedIndicator.Value < (LfzMaxSpeed - LfzMinSpeed) Then
        ' Speed down
        SpeedIndicator.Value = SpeedIndicator.Value + 2
    End If
    Me.Marker2Drop.SetFocus
    End Sub
    Anyhow the press of an arrowkey still moves the focus around, afterwards the focus is set to the selected control (Marker2Drop).
    It seems that the use of the arrowkey is directly moving the focus.
    So I'm still searching
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I myself use the normal Form events for key presses.
    When the user presses a key, I set a particular public boolean value true.

    Then each time the game screen is being updated it does whatever it has to do with the key in that state, and then resets the value.

    Take a look in the craft code for an example.
    You'd wanna look at the Form_Key* events for the main form, and also the main game loop in there too.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Sorry, but it doesn't do it.
    I get the key_events, but that's no change. the problem is still that a press of an arrowkey not only just a key_press that can be read , it's also a command to move the focus around the active form. Thereby shifting options, if the focus is moving along such an option-field!
    Damm it :-((
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well focus should only be shifted if a control currently has focus.
    Give the form itself focus, and also remove the TabIndex property of all the controls.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    ??
    Remove TabIndexProperty (whow do I do that), or only TabStop=false? The latter doesn't do it!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Right neither of those will work.
    Okay, if you add a picturebox to the form.

    Display the game in the picturebox, and give it focus.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    OKay, I do have a picturebox in my form. If I give it the focus just before checking the Arrowkeys, the problem concerning the shifted options is gone (Thanks).
    The sideeefect is that I'm now not able to make any input into textboxes, 'cause the focus will be lost. Maybe I have to stop the Timer2 during the time a textbox has the focus???? Just thinking loud.
    I'll try my very best, Miss Sophie!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I'd try to avoid using the textboxes if possible...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  12. #12

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    It works, it's a bit unusual that the focus can't be held by a control, but it works as desired.
    For all the controls which need an input (I have 1 textbox, and 5 comboboxes) I used the trick to disable to Timer2 while they have the focus and give it back when leaving. In my opinion it looks good. Why should I avoid textboxes?, I need some Input (I could use Comboboxes or listcontrols instead, but what's the difference?)

    Most of all, THANK'S . It took a long time for me to get that solved.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    my program is not really a game, it's of a training tool for some submarine hunting tactics
    Sorry, I'm just curious about that part

    Submarine hunting tactics? REAL submarines, like a training program for the navy or something?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  14. #14

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    O.K. here is a short description:
    The application is made to train in the use of "Dopplerinformation" to locate a submarine. In order to make that possible the sub must produce a stable frequency which the searcher can detect on selectable locations. The change in the frequency, due to the Doppler-Phenomena, enables the searcher to locate the sub without nay other information.
    In the application the searcher can select the positions where he wants to listen and also gets the frequency-information of those locations. I my special case the searcher is an aircraft (steering only for left/right and speed up/down), the sensor are socalled buoys, the frequency-informations is given on Frequency-Time indicators (Frequency horizontal, time verticaly given a picture of the actual and history information).
    There is no fancy visualisation, since no real pictures of aircraft nor submarines are needed. So no real "game", the clou only comes to those who learned such tactics and who wanted to keep trained in the use of them.
    It's not used by the "navy" (yet??), I'm working on it.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Do you have to kill us now ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Opus, that seems really cool, but I don't think the navy will make it official unless you know some people there, or you're in the navy
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  17. #17

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Jotaf98, something makes me think you're a student ;-)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  18. #18

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Thanks plenderj,
    the GetTickcount is controlling it now, using a setting of 500 (millisecs), the normal click_events are not blocked and the steering comands work as desired. However, there is a small problem, if I close the application no using the X in the right hand corner I get a runtime-error 5 at the line where the focus is set to a picturebox. This is not happening when closing normally via an end command.?????
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  19. #19
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well it could be due to the fact that the loop is still runnning even though the form is unloading.

    So if you do something like this :

    VB Code:
    1. Do While DoLoop
    2.     DoEvents
    3.     If (GetTickCount() - LastTick) >= 500 Then
    4.         LastTick = GetTickCount()
    5.         'your code here
    6.     End If
    7. Loop

    Then when you're going to be unloading the form for whatever reason, set DoLoop = False
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  20. #20

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Okay, but whre do I have to put code that is supposed to be used in case of the click on the "X" of the MainForm. That "X" just closes the form. Or is there an event Unload?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  21. #21
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Option Explicit
    2. Private closedByApp As Boolean
    3.  
    4.  
    5. Private Sub Command1_Click()
    6.     closedByApp = True
    7.     Unload Me
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     If Not closedByApp Then MsgBox "Someone clicked the x !"
    12. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  22. #22
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Opus: did you see my profile or something? It's not hard to figure that out, I'm 15

    As to your problem, it's easy, I think they already gave you what you need... a better way would be to have "End" in the Form_Unload event. That would be even better (and simplier) than what they're suggesting
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  23. #23
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Jotaf98
    Opus: did you see my profile or something? It's not hard to figure that out, I'm 15

    As to your problem, it's easy, I think they already gave you what you need... a better way would be to have "End" in the Form_Unload event. That would be even better (and simplier) than what they're suggesting
    "they" jotaf ?
    Whats that supposed to mean
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  24. #24
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hum, maybe you could trap key presses another way - here's how

    Btw, this demo is set up to work with keys F1-12, but you can easily use other keys, don't worry
    Attached Files Attached Files
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  25. #25

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Great Jotaf98, the link brought an attachment.php and not a *.zip file!

    Anyway, the problewm is solved, and I was stupid. No need to use a loop the set the focus to the picturebox, I'm setting the focus to the picbox now each time a click on an other control happens, that way the arrowkeys have no sideeffects and the change to another window is also not affected. I think we can close this one now.
    Thanks to both of you!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  26. #26
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    No problem, just leave the cheque on the way out.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  27. #27
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Yeah and the car's keys too

    Weird, the download works for me. Are you sure it's only this one that doesn't work? Try downloading other attachments in other posts. Also if you're using a downloader like FlashGet, GetRight or Go!Zilla, it won't work unless you have the latest version. Use IE or NS's downloader instead, after all the file is only 5 or 6 kb
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width