Detect Mouse Click and Keyboard key
Results 1 to 3 of 3

Thread: Detect Mouse Click and Keyboard key
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2018
    Posts
    196
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Detect Mouse Click and Keyboard key

    Assuming I have two codes, how can I wrap my code to detect if Enter key was pressed or a mouse click? So, if it is enter key it should run one code, if mouse click it should run another code

  2. #2
    Board Regular BlakeSkate's Avatar
    Join Date
    Jan 2015
    Posts
    277
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Detect Mouse Click and Keyboard key

    Quote Originally Posted by yinkajewole View Post
    So, if it is enter key it should run one code, if mouse click it should run another code
    where are they clicking (on a sheet? on a userform? a command button?), and where is the activecell upon pressing enter?
    Do you want these codes to run every single time someone clicks or presses enter?
    please also include sheet names where applicable
    -------------------------------------------------------------------------------
    I may not give the best VBA codes, but they sure are VBA codes.
    Help me help you by posting a snapshot of your data & your expected result
    Please use [ code][ /code] tags when posting VBA as well as proper indentation

  3. #3
    Board Regular BlakeSkate's Avatar
    Join Date
    Jan 2015
    Posts
    277
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Detect Mouse Click and Keyboard key

    but as a general answer you can use "application.onkey" in a worksheet sub where you activate the sheet and press enter anywhere on the sheet and then when you leave the sheet these codes deactivate so that you have normal functionality of enter.

    so lets say you copy this into sheet1's VBA module
    Code:
    Private Sub Worksheet_Activate()
        Application.OnKey "{ENTER}", "ACode"
    End Sub
    
    Private Sub Worksheet_Deactivate()
        Application.OnKey "{ENTER}", ""
    End Sub

    That will use the numeric enter key to execute a procedure
    and below is the regular enter key to execute a procedure

    Code:
    Private Sub Worksheet_Activate()
        Application.OnKey "~", "ACode"
    End Sub
    
    Private Sub Worksheet_Deactivate()
        Application.OnKey "~", ""
    End Sub
    clicking is a different sheet specific code as well
    but i would like more details before i really get into it.
    -------------------------------------------------------------------------------
    I may not give the best VBA codes, but they sure are VBA codes.
    Help me help you by posting a snapshot of your data & your expected result
    Please use [ code][ /code] tags when posting VBA as well as proper indentation

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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