dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Application.OnKey not working in a do-loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    20

    Resolved [RESOLVED] Application.OnKey not working in a do-loop

    Hello,
    This is my first try to use Application.OnKey
    Would someone please help me in the following code. I have a command button on Sheet 1 the following is supposed to work, but it does not!!


    Private Sub CommandButton1_Click()
    Dim MySelection As Integer

    Application.OnKey "{RIGHT}", MySelection = 1
    Application.OnKey "{LEFT}", MySelection = 2
    Application.OnKey "{ESC}", MySelection = 3

    Do
    Select Case MySelection
    Case 1
    MsgBox "Right Key selected"
    Case 2
    MsgBox "Left Key selected"

    Case 3
    Application.OnKey "{RIGHT}", ""
    Application.OnKey "{LEFT}", ""
    Application.OnKey "{ESC}", ""
    Exit Do

    End Select

    Loop Until MySelection = 3
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,778

    Re: Application.OnKey not working in a do-loop

    i think while the procedure is still running the keypress would be blocked

    you should set the keys in one procedure, then when a onkey is pressed it will start someother (or the same) procedure
    each onkey press must occur after any previous code has completed

    the onkeys set in your example will all look for a procedure named FALSE
    myselection = 0 so any other value will return false
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    20

    Re: Application.OnKey not working in a do-loop

    Quote Originally Posted by westconn1 View Post
    i think while the procedure is still running the keypress would be blocked

    you should set the keys in one procedure, then when a onkey is pressed it will start someother (or the same) procedure
    each onkey press must occur after any previous code has completed

    the onkeys set in your example will all look for a procedure named FALSE
    myselection = 0 so any other value will return false
    I tried adding myselection = 0 after each procedure .. but still not working. I'm not sure if I've understood you correctly .. please see below:

    Private Sub CommandButton1_Click()
    Dim MySelection As Integer

    Application.OnKey "{RIGHT}", MySelection = 1
    Application.OnKey "{LEFT}", MySelection = 2
    Application.OnKey "{ESC}", MySelection = 3

    Do
    Select Case MySelection

    Case 1
    MySelection =0
    MsgBox "Right Key selected"

    Case 2
    MySelection =0
    MsgBox "Left Key selected"

    Case 3
    MySelection =0
    Application.OnKey "{RIGHT}", ""
    Application.OnKey "{LEFT}", ""
    Application.OnKey "{ESC}", ""
    Exit Do

    End Select

    Loop Until MySelection = 3
    End Sub

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,778

    Re: Application.OnKey not working in a do-loop

    I'm not sure if I've understood you correctly
    i am very sure you have not

    the onkey press will not work until after that procedure finishes, though in fact, as myselection will never = 3 the procedure is an endless loop

    this is the closest example i could come up with, to achieve a similar result to what you were expecting, after you click command1 you can press left or right as many times as you want until you press escape

    Code:
    Private Sub CommandButton1_Click()
    Application.OnKey "{RIGHT}", "myright"
    Application.OnKey "{LEFT}", "myleft"
    Application.OnKey "{ESC}", "myend"
    
    End Sub
    Sub myend()
    Application.OnKey "{RIGHT}", ""
    Application.OnKey "{LEFT}", ""
    Application.OnKey "{ESC}", ""
    
    End Sub
    Sub myright()
    MsgBox "Right Key selected"
    
    End Sub
    Sub myleft()
    MsgBox "Left Key selected"
    
    End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    20

    Re: Application.OnKey not working in a do-loop

    Quote Originally Posted by westconn1 View Post
    i am very sure you have not

    the onkey press will not work until after that procedure finishes, though in fact, as myselection will never = 3 the procedure is an endless loop

    this is the closest example i could come up with, to achieve a similar result to what you were expecting, after you click command1 you can press left or right as many times as you want until you press escape

    Code:
    Private Sub CommandButton1_Click()
    Application.OnKey "{RIGHT}", "myright"
    Application.OnKey "{LEFT}", "myleft"
    Application.OnKey "{ESC}", "myend"
    
    End Sub
    Sub myend()
    Application.OnKey "{RIGHT}", ""
    Application.OnKey "{LEFT}", ""
    Application.OnKey "{ESC}", ""
    
    End Sub
    Sub myright()
    MsgBox "Right Key selected"
    
    End Sub
    Sub myleft()
    MsgBox "Left Key selected"
    
    End Sub

    When I run this code, I received the following message:
    Cannot run the macro "......'!myend'. The macro may not be available in this workbook or all macros may be disabled.

    Even though my settings is all macros enabled !!
    Attached Images Attached Images   

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,778

    Re: Application.OnKey not working in a do-loop

    as your procures are in an object module, you should specify the module like
    Code:
    Application.OnKey "{RIGHT}", "sheet1.myright"
    alternatively move the procedures to standard module
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    20

    Re: Application.OnKey not working in a do-loop

    Quote Originally Posted by westconn1 View Post
    as your procures are in an object module, you should specify the module like
    Code:
    Application.OnKey "{RIGHT}", "sheet1.myright"
    alternatively move the procedures to standard module


    Now it is working .. thanks a lot for your support.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width