+ Reply to Thread
Results 1 to 2 of 2

How to get Last Key or Key-Combo Pressed (Not Event)

  1. #1
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    How to get Last Key or Key-Combo Pressed (Not Event)

    hi

    i'd like to get the last key or key-combo pressed.
    Windows API method is fine, or VBA.

    Note: I need to get this several seconds AFTER they key is pressed. I do not care about the current state of the key. I'm not trying to run a macro in response to a keypress. So, getasynckeystate and OnKey are NOT the appropriate solutions for this. The key might be pressed while a macro is running, and i want to allow the macro to finish running without interruption, and only get the last key pressed after the key has already been released, possibly several seconds before.

    I'm not trying to detect any specific key, just want to get last keypress, whatever key it was.

    The method should be able to detect special keys, like home, end, left, right, down, and up.

    It should also recognize key combinations, like control alt and shift.

    Using Excel 2007.

    Thanks!

    also posted here:
    http://www.vbaexpress.com/forum/showthread.php?p=273634
    Last edited by johnywhy; 08-02-2012 at 10:05 PM.

  2. #2
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    Solved

    hi

    not sure how to mark this solved, but I got it. The neat thing about this solution is that if you put a loop around it, you can get all keystrokes in the buffer, not just the most recent. Even better, you can get mousecklicks.

    The answer is the API function, PeekMessage.

    Here's the declaration:
    Please Login or Register  to view this content.
    Example call:
    Please Login or Register  to view this content.
    You also need the TranslateMessage and FindWindow fuctions, constants, message data-structures, and a few other bits.

    A full example:
    http://www.mrexcel.com/forum/showthr...=1#post1264669

    Doc:
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1