Microsoft Access - Custom Behaviour for Keystrokes

gkluk
gkluk used Ask the Experts™
on
Dear All,

I am on Access 2016 and I would like to run some custom code when the user clicks on Shift and P (holds down Shift and then press P). I cannot use AutoKeys as +P does not work and Access tells me to use SendKeys, so how can I do it (SendKeys does not work in this case, as it is only sending the keystrokes, but not instructing Access how to respond to them).

Regards,
George
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
Someone may have the answer but I'm pretty sure that if SendKeys won't work, then you are out of luck unless you have the ability to write or purchase a TSR (Terminate and stay resident) program that perpetually sits in memory and looks for this combination of keystrokes.  What it does with them is a different issue.

Exactly what is the problem you are trying to solve?  Perhaps the solution is to disable the shift and control keys (if that is even possible).  If that is possible, then you can provide a print option for the report and when the user chooses that option, you can log that the report was printed.

However, there is absolutely NO way to determine if something actually got printed.  If the printer is out of paper or out of commission or gets jammed and wrecks half the report, the print job stays in the print queue and is flushed on a reboot.  So, whatever you are trying to track, intercepting the keystrokes can only determine intent and not actual print complete.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Hi,

To my knowledge, you can only intercept keystrokes from control event handlers (according they support keyboard input (some don't), namely KeyPress, KeyDown and KeyUp events.

Maybe you can have a modal form sized as small as possible (so it is almost not visible) with a unique TextBox control.
But that smell akward programming, as you'll have to forward events to whatever supposed real target (daunting and laborious task).
Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Commented:
Yes, Access reports that for any sequence SHIFT+{key}.

Why not do CTRL+SHIFT+{key] instead?
^+P

Open in new window

Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
As Daniel said, use the control key in combination with the P instead of shift.

Using the shift key is problematic because it's normal typing.  Anytime someone typed a capital P, your code would fire.

 What is it that your trying to accomplish?

Jim.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
Have you tried enabling the form KeyPreview, and then using the KeyDown event of the control or form to test for that combination.  You can do that and then set the KeyCode value to 0 to basically erase that keystroke from the buffer.

Dale

Author

Commented:
@All: I agree with the comments that there are other ways of achieving this, for example doing a three-key combination or using events and, effectively, cancelling out keystrokes? However, my original question remains out of academic curiosity: regardless of the alternatives and how much sense they make or not, would AutoKeys take +P possibly with some configuration changes or Dale's seem to be the only solution if someone wants to go ahead and intercept the Shift+P key combination?

Regards,
George
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
Auto Keys will take Ctrl/P and work.

Jim.

Author

Commented:
@Jim: Apologies, typed too fast in my previous comment, I meant Shift and P (will try to edit it now).

Regards,
George
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
No with shift/P, dale’s suggestion is the only one that would work.

  Auto keys does not allow it because it just doesn’t make sense that you’d want to carry out an action by using the shift key,  which gives you capital letters.  

Jim

Author

Commented:
Fair enough, I will give Dale's submission a go and report back (apologies for the late replies, last days before Christmas at college are always busy with coursework marking and the like)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial