Search code examples
excelvbscriptkeypressonkeypress

Excel – Detect if 2 specific keys being pressed at once


I don’t know if this is possible. I am working with Excel and using VB script. Is there a way to detect if two keys are being pressed at the same time and which ones they are? Then if it is those keys that are pressed I can use an If/Then statement to do whatever processes I need to do?

I know if I have something like a combo box I can use the keydown feature to detect a single key pressed, but this will only work on one key and not two keys. Also, I am not using combo boxes, text boxes, or anything else. I am strictly using cells, so there doesn’t appear to be anything like keydown to detect even the press of a single key.

Again, I would need for it to detect two keys at the same time being pressed. I would also somehow like to get it to detect this at the workbook level as well, instead of each individual worksheet, since there are several worksheets and would like these pressed keys to work from one sheet to the next.

Please let me know if this is possible or not, but I have a feeling it is not possible.


Solution

  • Doug,

    Thanks for your suggestion, I figured everything out due to that. Here it is, in case someone else finds this useful:

        Private Sub Workbook_Activate()
    
        'When the workbook is active this will run the script in the place of the standard Ctrl + C.
        Application.onkey "^{c}", "ThisWorkbook.cCopy"
    
        End Sub
    
    
        Private Sub Workbook_Deactivate()
    
        'When another workbook is active this will disable this script so the standard Ctrl + C will work again.
        Application.onkey "^{c}"
    
        End Sub
    
    
        Sub cCopy()
    
        'This is the script to run when active. This was used for testing purposes only.   
        Worksheets("Sites").Range("I1").Value2 = "Yes"
    
        End Sub