Onkey Event is not working

Anu230

New Member
Joined
Dec 13, 2013
Messages
7
Hi,

I want to create a macro in which an event gets triggered whenever printscreen button is pressed.

U tried to do Application.Onkey but its not working. not only with printscreen button application.Onkey is not working with any button.
can anyone tell me why it is not working

Ex: with PGDN button also its not working
Application.OnKey "{PGDN}", "Printscreen"


Thanks in Advance:)
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Where is this located:

Application.OnKey "{PGDN}", "Printscreen"

And how is it executed?

Where is your Subroutine called Printscreen located and what does it look like?
 
Upvote 0
My printscreen() is located in the same module.. I was just testing if onkey is working or not. so i haven't added anythng in der except a msgbox with message to know that its working fine. but nothing is happening after clicking PGDN button..
 
Upvote 0
Hi,

I want to create a macro in which an event gets triggered whenever printscreen button is pressed.

U tried to do Application.Onkey but its not working. not only with printscreen button application.Onkey is not working with any button.
can anyone tell me why it is not working

Ex: with PGDN button also its not working
Application.OnKey "{PGDN}", "Printscreen"

AFAIK, .OnKey requires two keys pressed at minimum. From VBA Help (bolding added):

The Key argument can specify any single key combined with ALT, CTRL, or SHIFT, or any combination of these keys. Each key is represented by one or more characters, such as <CODE>"a"</CODE> for the character a, or <CODE>"{ENTER}"</CODE> for the ENTER key.

In other words, you must have ALT, CTRL, SHIFT (or a combination of these specific keys) already pressed when your "hook" (for lack of a better word on my part) key ("{PGDN"} in your example) is pressed, for Windows to "catch" it.

Hope that helps,

Mark
 
Last edited:
Upvote 0
Yikes! I should never post when I'm that tired...

Apologies, as I was incorrect and failed to test, on top of being forgetful. Anyways, in a Standard Module:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> SubstituteKey()<br>  MsgBox "key pressed"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> OnKey_On()<br>  Application.OnKey "d", "SubstituteKey"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> OnKey_Off()<br>  Application.OnKey "d"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Does that help?

Mark
 
Upvote 0
Thanks mark.. its working fine.. May i know how Onkey_Off sub works and y is it used here?

Happy New Year..:)
 
Upvote 0
Hi there,

Sorry for the slow reply. If you omit the second argument (the name of the procedure to run), then the key or key combination reverts to whatever Excel normally does. For instance, if you press CTRL+F9, the active workbook would minimize. If you have re-assigned this key combo to another procedure, I try and make sure to reset to the default behavior before closing the workbook (or activating another wb, or whatever seems appropriate).

From vba Help:

Procedure </SPAN> [Optional] A string indicating the name of the procedure to be run. If Procedure is "" (empty text), nothing happens when Key is pressed. This form of OnKey changes the normal result of keystrokes in Microsoft Excel. If Procedure is omitted, Key reverts to its normal result in Microsoft Excel, and any special key assignments made with previous OnKey methods are cleared.

Hope that helps,

Mark
</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top