+ Reply to Thread
Results 1 to 12 of 12

Thread: Capturing Keys

  1. #1
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Question Capturing Keys

    How do I capture keys sent to Excel when my workbook is open / active?

    I know when you write a macro you can assign a shortcut key combo to it, such as "Ctrl-Shift-s", but I can't find where / how Excel stores that information, and can only presume it is stored away not accessible to the coder, (ie. not by going via the macro -> properties boxes).

    My issue is I want to capture a key combo, then from that call a function passing a variable dependant on the combo pressed, eg:

    Code:
    Function DispMessage(themessage As String)
    MsgBox themessage
    End Function
    Code:
    xxxxxxxxx
    Capture Ctrl-Shift-s
    DispMessage "Ctrl-S was pressed"
    xxxxxxxxxxxxxxx
    Capture Ctrl-Shift-b
    DispMessage "Ctrl-B was pressed"
    Thanks
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  2. #2
    Valued Forum Contributor
    Join Date
    05-15-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    797

    Re: Capturing Keys

    Would you be able to use the .onkey

    E.G
    Code:
    Sub SetShortcutKeys()
    With Application
    .OnKey Key:="^+K", Procedure:=ScreenPaste
    End With
    End Sub
    I tryed just the other night to assign a shortcut key to a macro in xl2007 and couldnt figure it out. It seams diffrent that what i found on the net.

  3. #3
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,431

    Re: Capturing Keys

    It is stored in the actual code, look at the recorded macro in the VB Editor
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  4. #4
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Capturing Keys

    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 19/10/2009
    '
    ' Keyboard Shortcut: Ctrl+Shift+S
    '
        ActiveCell.FormulaR1C1 = "I pressed Ctrl-Shift-S"
        Range("A2").Select
    End Sub
    I understand that Roy, but I can remove that comment line, and press Ctrl-Shift-S and the macro will run again, so it is not THAT that is controlling which macros are run by which key combos.

    I shall D_Rennie's code, thanks for that
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  5. #5
    Valued Forum Contributor
    Join Date
    05-15-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    797

    Re: Capturing Keys

    Here is what keeped popping up for me in the google searches last night.

    select.. Tools,,, Macros, Select the macro,, Push buttion macro options. There will be the assigned macro short-cut key.

    Though for the life of me i that options buttion is not there, Nore anywhere in the Editor.

  6. #6
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Capturing Keys

    Quote Originally Posted by D_Rennie View Post
    Would you be able to use the .onkey

    E.G
    Code:
    Sub SetShortcutKeys()
    With Application
    .OnKey Key:="^+K", Procedure:=ScreenPaste
    End With
    End Sub
    I tryed just the other night to assign a shortcut key to a macro in xl2007 and couldnt figure it out. It seams diffrent that what i found on the net.
    Cheers, I had to put quotes around the procedure name, but other than that it works very well

    Code:
    .OnKey Key:="^+K", Procedure:="ScreenPaste"
    thanks again!
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,941

    Re: Capturing Keys

    I don't belive you can access the information via the OM.

    You would need to export the module to a .bas file and then read the text file to discover what values have been set for VB_Invoke_Func

    Macro1 has the CTRL+k shortcut.
    Code:
    Attribute Macro1.VB_ProcData.VB_Invoke_Func = "k\n14"
    Cheers
    Andy
    www.andypope.info

  8. #8
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Capturing Keys

    Quote Originally Posted by D_Rennie View Post
    Here is what keeped popping up for me in the google searches last night.

    select.. Tools,,, Macros, Select the macro,, Push buttion macro options. There will be the assigned macro short-cut key.

    Though for the life of me i that options buttion is not there, Nore anywhere in the Editor.
    That is certainly the way to do it manually in 2003, I suppose it is another thing that has changed in 2007 though...
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,941

    Re: Capturing Keys

    for xl2007 check the Developer tab. The Macro button displays the old dialog with Options available.
    Cheers
    Andy
    www.andypope.info

  10. #10
    Valued Forum Contributor
    Join Date
    05-15-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    797

    Re: Capturing Keys

    for xl2007 check the Developer tab. The Macro button displays the old dialog with Options available.
    Guess i was just looking in the wrong place. Seams silly to have the two diffrent though.

    export the module to a .bas file and then read the text file to discover what values have been set for VB_Invoke_Func
    learn somethink new every day.

    Cheers

  11. #11
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Capturing Keys

    Oops, my bad, it doesn't do what I was after, as it will still only let me assign a procedure to it that doesn't take arguments

    As an example, lets say I want ctrl-shift-a to perform 12+5 and display a message box, and ctrl-shift-s to perform 12-5 and display a message box.

    I have a procedure:
    Code:
    Sub DoTheMath(addition As Boolean)
    If addition Then
      MsgBox 12+5
    Else
      MsgBox 12-5
    End If
    I was wanting someway to write the code that would allow the key-combo ctrl-shift-s to call "DoTheMath False", but there doesn't seem be anything.

    I know I could have 2 seperate 'helper' subs, as below, but that pretty much defies teh point of what I was trying to do:
    Code:
    Sub DoAdd()
    ' Shortcut ctrl-shift-a
    DoTheMath True
    End Sub
    
    Sub DoSub()
    ' Shortcut ctrl-shift-s
    DoTheMath False
    End Sub
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,941

    Re: Capturing Keys

    Seeing as you are assign to sets of keystrokes I can not see why separate routines is a problem.

    You will not be able to pass an argument if you are using keyboard shortcuts.
    Cheers
    Andy
    www.andypope.info

Thread Information

Users Browsing this Thread

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

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.2.0