+ Reply to Thread
Results 1 to 7 of 7

Catching keyboard events

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Catching keyboard events

    I'm looking for a way to catch all keyboard events. I need to test the state of a Ribbon button whenever a key is pressed so that I can run other code based on the state of that Ribbon button. The key or combination that is pressed is not important, just the state of the Ribbon button when any key has been pressed.

    I have a Custom Ribbon that has a button which uses the PasteValues IDMSO and so it's enabled state is controlled by Excel. I have another button that should only ever be enabled when this PasteValues button is enabled. I have code that controls the button sufficiently when Paste Values becomes enabled or disabled through Excel's UI but this does not work when it is enabled or disabled through keyboard shortcuts.

    Whenever a key is pressed, I want to test the state of the Paste Values button and enable or disable my other button accordingly. It is not important that key strokes be monitored when Excel is in 'cell edit' mode or a userform is running etc.

    I could go a different route here and use application.onkeys to remap what specific key combinations like "shift+insert" or "ctrl+v" do but there's always the chance that something gets missed. For instance, when the Paste Values button is enabled, just editing a cell is enough to disable it as editing the cell changes the CutCopyMode property on entry. Since any key on the keyboard (effectively) can put Excel into 'cell edit' mode I think I really need to be monitoring the state of the Ribbon Button when any key was pressed.
    Please click *Add Reputation if I've helped

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Catching keyboard events

    Hi kadeo,

    The following may help you get started. The code was tested and working using 32 bit Excel, but has never been compiled using 64 bit Excel.

    The file contains the following code:
    Please Login or Register  to view this content.


    Here is additional code which may help you trap special keys. This code has never been tested in conjunction with the code in the file:
    Please Login or Register  to view this content.
    Lewis
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Catching keyboard events

    Thanks for the reply Lewis

    Yeah, I'd seen that a few times when I was Googling. It seems real convoluted for what I need though. I know in a userform i can use the likes of keydown and keyup. It just seems real strange to me that something so basic (seemingly to me anyway) isn't covered for in normal sheets.

    Still, I live in hope that somebody has a simple workaround.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Catching keyboard events

    I know in a userform i can use the likes of keydown and keyup
    I was hoping for that too. I haven't found the equivalent of Userform KeyDown and KeyPress events. After extensive searching a while ago, the code I posted was the best I could find.

  5. #5
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Catching keyboard events

    Is there anybody else can help now there's been a week to mull it over?

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Catching keyboard events

    There isn't a simple way, what Lewis has given you is the only way

  7. #7
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Catching keyboard events

    Unreal! Thanks Kyle. It sucks but I guess that's this thread solved. Thanks for your help too Lewis!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] XML VBA Error Catching
    By Inti in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 07-10-2014, 02:29 PM
  2. observe keyboard events in Excel 2010
    By forumarbeit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2014, 01:23 PM
  3. Catching '#REF!' errors
    By enchufla in forum Excel General
    Replies: 2
    Last Post: 12-04-2012, 09:48 AM
  4. If stmt not catching everything
    By ritz in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-28-2007, 12:32 PM
  5. Replies: 1
    Last Post: 02-08-2007, 10:57 AM
  6. Catching an error
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-04-2006, 05:15 AM
  7. Catching errors
    By cbh35711 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-03-2006, 04:52 PM

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