Results 1 to 8 of 8
  1. #1
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74

    Wait for multiple keystrokes before executing code

    Hi all, I have code that runs an event when you hit Page Up or Page Down on the keyboard. It works great. The only thing I'd like to change is have it wait one second before executing so if I hit page down 3 times, only run the code the third time.



    Here's the code. It basically tracks where you are scrolled and resets the default value of one of the fields depending on what the last visible item on the page is.

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 33 Then
        SUp Forms!mainmenuf!ItemsPerPage
        KeyCode = 0
        ResetDefault
    End If
    
    
    If KeyCode = 34 Then
        SDown Forms!mainmenuf!ItemsPerPage
        KeyCode = 0
        ResetDefault
    End If
     
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    What do you expect to happen after 3 key presses, move 3 pages? One page?
    Introducing a pause isn't difficult, but I'm not seeing how that will help. As you have it, such a pause will only slow down what's already happening. Based on your thread title, it's counter intuitive to think you can stop key press code from running just because there's multiple calls to it.

    There may be a way to redirect execution when there are multiple presses of the same key by using the Shift parameter, but I don't see the point in scrolling only 1 page when there were 3 presses.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74
    Exactly. I want it to jump straight to the third page if you hit page down three times quickly. I am hoping for a way to record how many times page down was pressed and scroll down that many pages all in one shot. Right now if you hit it 3x, it works fine and it will scroll down the three pages, it just runs slow clunky code between each page down which isn’t necessary.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    The problem as I see it is, adding a pause as soon as the code starts can be done but if you think it's slow/clunky without it, adding a delay just seems a step backwards even if it were possible, which I suspect is not. The problems being that a) the event cannot be canceled and b) has to run its course - you cannot start subsequent press events while the 1st is "on hold", so subsequent press events cannot check to see if subsequent presses have occurred. If that's not clear, then putting it another way, the code must run from start to finish. You can delay this execution, but cannot branch out of it to detect if the key was pressed again, then branch out of the next one to check for another keypress, and so on, then go back into each one in turn and complete the execution. At least I know of no possible way to do that and I see that as the only way it could work in your current setup.

    I would investigate the use of the shift parameter with this half-baked notion in mind:
    Users are taught to first hold shift key to accumulate multiple pages, then press once more without the shift key.
    Each iteration of the code adds +1 to a module level counter IF the shift key hold down is true, then exits the procedure.
    If an iteration of the code is run without the shift, then add +1 to the counter which will either be 0 or some other number at that point. Use a for next loop to scroll that many pages.

    I suppose the only other approach would be a method of creating a number of pages to scroll (such as a spinner control) and moving that many pages on a button click, which is entirely outside of your current process.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    not tried, but perhaps you can use the keydown event to set a timer interval and suppress the action, then run the page move in the timer event. Pure aircode and not in a position to make it work but something like this in the keydown event

    have a module level variable called say pagecount

    Code:
    if me.timerinterval=0 then
        me.timerinterval=300 (0.3 of a second)
        pagecount=0
    end if
    
    If KeyCode = 33 Then
        pagecount =pagecount-1
    elseif KeyCode = 34 then
        pagecount=pagecount+1
    end if
    
    KeyCode = 0
    and in the timer event, code to move the pagecount number of pages. means user waiting 0.3 of a second for a single page, but they will barely notice it

  6. #6
    redbull's Avatar
    redbull is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    If you want to just do something on the 3rd press of a button... keep track of how often that button was pressed... This would go inside your if key = this statement I'm sure you are using
    Code:
    Static KeyCount as integer 'Put this at the top of your sub, not within the if
    If KeyCount = 3 then
     'Do stuff
     KeyCount = 0
    Else
     KeyCount = KeyCount + 1
    End if
    Static means the variable is stored in memory, so even if the code stops it will retain the value of whatever.

    I just re-read your question, this might not be the solution you are looking for.. but I'm leaving just the same. Hope you can use it.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Example code that performs an action after so many keystrokes http://allenbrowne.com/ser-32.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74
    Thanks everyone! None of these quite solve the issue (because I still want the code to fire after one press if it’s not immediately pressed again) but we’re moving in the right direction with some good ideas. I’ll keep researching.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 09-12-2018, 04:12 AM
  2. Making code wait/pause until event
    By faythe1215 in forum Programming
    Replies: 3
    Last Post: 02-09-2015, 03:44 PM
  3. Query not properly executing within VBA code
    By kewelch in forum Queries
    Replies: 1
    Last Post: 07-17-2013, 01:12 PM
  4. Code executing without errors, but not working
    By The Professor in forum Programming
    Replies: 13
    Last Post: 02-18-2013, 04:09 PM
  5. Problems Executing SavedQuery in Code
    By RMittelman in forum Programming
    Replies: 4
    Last Post: 06-18-2011, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums