UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> how to trigger a keyboard event    
 
   
Hein
post Oct 25 2007, 01:40 PM
Post #1

UtterAccess Veteran
Posts: 384
From: The Netherlands



Hi all

Since I have my hands on the keyboard most of the time I would like to capture a few mouse actions with code.

I have searched the forum and my books for clues but kind find what I want.

Just to learn the trick I would like to hide/unhide columns in my subForm
Now I have the code for it in a toggle button that does the trick.

I would like to use the keycombination control-shift-F5 to do this (it will simply call the now existing procedure)

I have found a list with Keycode Constants (eg vbKeyF5) but don't know how to use it.

Who can help me out?

Thanks in advance
Hein

PS
Will this slow my database down because now every key-action will be evaluated by Access?
Go to the top of the page
 
+
jasonlewis
post Oct 25 2007, 01:48 PM
Post #2

UtterAccess Addict
Posts: 201



Try Keboard Macros:

CODE
Application.SetOption "Key Assignment Macro", "mcrKeyboard"


Where mcrKeyboard refers to a macro, which allows for the Macro Name field to be used as the key-press combinations that will run a macro (which can then of course run code):

i.e.
Macro:
--Name: mcrKeyboard

'Macro Name'--'Condition'--'Action'--'Comment'
+^F12 -- --RunCode: FunctionName=MyFunction()--Run my function

Note the +^F12 refers to Ctrl-Shift-F12 used to execute action indicated.

Hope this helps,

Jason
Go to the top of the page
 
+
Hein
post Oct 25 2007, 02:05 PM
Post #3

UtterAccess Veteran
Posts: 384
From: The Netherlands



Hi Jason,
Thanks for your reply

Is it possible to capture this with vba
1 of the reasons for this is that in a macro I can't capture errors. The other is that I am trying to learn to use vba.

Hein
Go to the top of the page
 
+
jasonlewis
post Oct 25 2007, 04:31 PM
Post #4

UtterAccess Addict
Posts: 201



Hi Hein,

The keyboard macro doesn't prevent you from using VBA for the action you wish to take associated with the keyboard sequence (i.e. Ctrl-Shift-F5). Note in the example the Action taken was to execute a VBA function named MyFunction(). Do what you want using VBA inside the MyFunction() function in VBA. o!

There is a lot more programmatic complexity to capturing the keystrokes within Access outside the above method. Not impossible, just more complicated. (Unless they added a new way to allow VBA to be associated as a keyboard handler directly, which I am unaware of). Based on your requirements as stated of wanting to use VBA to do things within Access when certain key combinations are detected (i.e. Ctrl-Shift-F5), I would suggest the above method -). I have used it in production environments with older versions of Access without issues (with VBA code being executed to handle the keyboard event).

Hope this helps,

Jason
Go to the top of the page
 
+
Hein
post Oct 25 2007, 04:52 PM
Post #5

UtterAccess Veteran
Posts: 384
From: The Netherlands



OK
I will try your suggestion
It's getting kinda late here in The Netherlands so tomorrow I'll report back to you how things are going

For now thanks for your help
Hein
Go to the top of the page
 
+
freakazeud
post Oct 25 2007, 05:35 PM
Post #6

UtterAccess VIP
Posts: 31,413
From: NC, USA



Hi,
I think what Jason is trying to refer you to is the AutoKeys macro. It's not called a keyboard macro. Look up autokeys in the Access help and it will give you sample syntax of how to implement it. It's basically a macro that specifies keyboard shortcuts and actions to take when they are pressed. The macro itself needs to be named AutoKeys...once it is it will execute whatever you want based on the shortcut pressed. Furthermore, form's have keypress/key down events if you want to utilize those on specific forms.
HTH
Good luck
Go to the top of the page
 
+
jasonlewis
post Oct 25 2007, 07:37 PM
Post #7

UtterAccess Addict
Posts: 201



Hi,

Well, you are right, it isn't called a keyboard macro, but it is (or was) called the 'Key Assignment Macro'. Apparently this was changed in later versions of Access (i.e. Access 2.0 I believe stored this in the database). This setting still works in Access 2003, but it is stored in the HKEY_CURRENT_USER key of global Access settings.

HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Access\Settings\Key Assignment Macro

The following code will result in the adjustment of the above registry value to "AutoKeys", and consequently it would appear if that macro exists in your application then it will be used as the keyboard macro.

CODE
  
Application.SetOption "Key Assignment Macro", "AutoKeys"


To avoid issues with other applications due to the 'Access-Global' nature of this setting, I agree that creating the macro named AutoKeys would be the way to go. I think Microsoft made this feature less desirable by changing the location where this setting was stored from the MDB to the registry. (i.e. Why use the registry when you have a perfectly good MDB file in which to store/retrieve application settings).

The KeyPress / KeyDown events may also be the way to go depending on your needs. Note they are different (and the differences can be subtle if you're not familiar with them), so read the documentation related to each carefully. They would keep you in VBA if that is your goal.

Jason
Go to the top of the page
 
+
Hein
post Nov 2 2007, 03:21 AM
Post #8

UtterAccess Veteran
Posts: 384
From: The Netherlands



Hi, sorry for not responding sooner

Freakazeud mentions in his last post:
"
Furthermore, form's have keypress/key down events if you want to utilize those on specific forms.
"

I know this, but my problem is that I don't know how to activiate it.

In short
With the key combination ctrl-shift-F5 I want to hide/unhide column A
With the key combination ctrl-shift-F6 I want to hide/unhide column B

Both procedures are now triggered with a button, I just want to call the procedure from these buttons


Thanks
Hein
Go to the top of the page
 
+
mishej
post Nov 2 2007, 03:39 AM
Post #9

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Subforms can be tricky. If the user presses Ctrl-Shift-F5 while a control on the main form has the focus then I would expect nothing should happen. Is that correct?

But if a control on the subform has the focus then that key combo would hide the column that has the focus?

First, in forms and reports, you can't use Keyboard events unless you set the Key Preview property to True. That may be the solution to your current problem. If you want to test (use) the Ctrl key then I'd suggest you use the KeyDown event and look at the online help for it.
CODE
Private Sub KeyHandler_KeyDown(KeyCode As Integer, _
     Shift As Integer)
    Dim intShiftDown As Integer, intAltDown As Integer
    Dim intCtrlDown As Integer

    ' Use bit masks to determine which key was pressed.
    intShiftDown = (Shift And acShiftMask) > 0
    intAltDown = (Shift And acAltMask) > 0
    intCtrlDown = (Shift And acCtrlMask) > 0
    ' Display message telling user which key was pressed.
    If intShiftDown Then MsgBox "You pressed the SHIFT key."
    If intAltDown Then MsgBox "You pressed the ALT key."
    If intCtrlDown Then MsgBox "You pressed the CTRL key."
End Sub

You see that you can check the Shift var to see if Ctrl is pressed.

Hope that helps.
Go to the top of the page
 
+
Hein
post Nov 2 2007, 04:35 AM
Post #10

UtterAccess Veteran
Posts: 384
From: The Netherlands



Hi John
Thanks for your answer.
I have tried solution and it worked.
After this I tried to change the code more to my situation and came up with:

If intCtrlDown And intShiftDown And vbKeyF5 Then MsgBox "You pressed the CTRL shift F5 key."

Now 2 things happen
1. After pressing ctrl and shift I get the message box. If I keep them pressed and press the spacebar the msgbox disappears
2. After pressing F5 the messagebox appears again (which is what I expected.

How can I get rid of the 1st event?

Hein

PS
The other lines intshiftDown and altShiftDown are "out of order" now
Go to the top of the page
 
+
mishej
post Nov 2 2007, 12:58 PM
Post #11

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Here is how I did it. I opened up my subform in design mode and selected View, Properties.

Make sure you click on the form so that the combo box at the top of the Property Sheet says Form.

Then find the Key Preview and set to Yes and the KeyDown event and set that to Event Procedure and then add this code to the form:
CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    Dim intShiftDown As Integer
    Dim intCtrlDown As Integer

    ' Use bit masks to determine which key was pressed.
    intShiftDown = (Shift And acShiftMask) > 0
    intCtrlDown = (Shift And acCtrlMask) > 0
    
    ' Display message telling user which key was pressed.
    If intShiftDown And intCtrlDown And KeyCode = vbKeyF5 Then
        MsgBox "Ctrl, Shift and F5 held down."
    End If
End Sub


That should do it. I tested this in Access 2000.

Good luck on the project.
Go to the top of the page
 
+
Hein
post Nov 5 2007, 02:15 PM
Post #12

UtterAccess Veteran
Posts: 384
From: The Netherlands



John,
I'm getting there, thanks to your help!

I would like to expand on what we now have

I have a mainform with 2 buttons to make columns in the subForm visible/invisible
With the code you made I simply call the code behind these buttons.
This works if I am still in the mainForm (as it should)

However, when I am on the subForm the code doesn't work

I have tried to make the code behind the buttons Public (Still within the mainForm) and I have tried to "direct" the code to the mainform with . and ! (e.g. call me.forms.Mainform.btnHide or call me!forms!Mainform!btnHide) but this doesn't work.

I made it work by writing the same (or more or less) code for the subForm, but it would be nice to have the same code just in 1 place.

Is there a way to make it happen

thanks in advance
Hein
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 24th May 2013 - 03:44 AM