Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Catching keyevents
#1
Hi. We've been looking for a way to catch pressed keys from the browser however, we can't find a way to do this without javascript. 
We did find an undocumented function called browser_OnKeyPress(Key as Integer) but this one doesn't seem to work at all. No matter how many times we press a key, it doesn't get activated. 

Is there a way to catch or redirect a keypress first to VBA/Access without javascript? 
We're using the activex version.

Thanks in advance.
Reply
#2
Hi,

Yes and no.

Officially Microsoft does not support to catch key events, however they have exposed a way to catch short cut keys.
This is exposed in the next release of AntView via the AntViewPhysicalKeyStatus interface.

I will email you a link to the private version so that you can take a look.

--
Wil
Reply
#3
Thank for the new version. I've tried it but it seems like it's not supported in VBA. When i want to implement it, it says that the function or interface is marked as reserved or is not supported by Visual Basic.
See: https://learn.microsoft.com/nl-nl/office...6rd%3Dtrue
Reply
#4
Hi,

I just verified in the code of the AntView control and the interface in the control is marked as public and AFAICT should be accessible by MS Access.

For MS Excel I remember that it creates small cache files of an ActiveX control (.exd files) when you first use it in MS Excel.
When you upgrade an ActiveX you have to get rid of those old caching files in order to use the new functionality the control might expose. I even adjusted the installer to get rid of those caching files -- when you're using MS Excel.
See https://doc.antview.dev/hs95.htm

Could it be that MS Access has a similar mechanism?

--
Wil

text was edited to be more clear.
Reply
#5
Hello Wila,

The functionality is visible, but when i click on it it just shows an error message that its marked as restricted or that the automation type is not supported. Same goes for Excel.
Access does not have such temp file. I've also tried creating a new Access app, reinstalling and registering the files manually with no success. The function/method is available, but it can't be implemented.

Just to be sure, we're both talking about the "OnAccelatorKeyPressed"-function right?
Reply
#6
(2023-01-02, 08:21:44)hsacrm Wrote: Hello Wila,

The functionality is visible, but when i click on it it just shows an error message that its marked as restricted or that the automation type is not supported. Same goes for Excel.
Access does not have such temp file. I've also tried creating a new Access app, reinstalling and registering the files manually with no success.  The function/method is available, but it can't be implemented.

Just to be sure, we're both talking about the "OnAccelatorKeyPressed"-function right?

Hi,

Yes, we're both talking about the new OnAcceleratorKeyPressed event.
Thanks for the clarification, let me see if I can reproduce your issue and figure out what is going on.

This was implemented based on your question here: https://forums.antwise.com/showthread.ph...408#pid408
so it would be sad if you are the one who then can not use it.
FWIW, it does work OK in other environments, apparently something is different in MS Access.

I will report back once I get a chance to look into this.
--
Wil
Reply
#7
(2023-01-02, 11:50:42)wila Wrote: I will report back once I get a chance to look into this.

Ok, I was able to reproduce this issue.
Interesting..
   

Turns out that MS Access does not like data type unsigned long for the VirtualKey parameter.
It's a bit ironical as parameter KeyEventLParam is a long.
Also seeing this same issue in the AntViewPhysicalKeyStatus interface for params RepeatCount and Scancode.

Hmm.. I guess we can drop back to a long data type on these parameters.
It is not perfect, but what is perfect these days?

I'm really glad we found this before the next official release.

You will be getting an email with a new private build to test from me in a bit.
--
Wil
Reply
#8
Yes, it works! Thank you very much.

I've played a bit around with it. The behaviour is somewhat different than what i expected.
Am i correct when saying that the keyaccelator only work when pressing a key modifier? (Ctrl, alt etc)
The way it activates may bring some difficulties, to catch ctrl + s for example. The function gets called allot of times when holding down the ctrl-key (which even lead to a crash once).
We can reduce it by checking if the key is released. It will then get called twice. Once for the ctrl-key and once for the S-key. So i think we have to build something to wait till the key get released and check what was pressed in the meantime.

We have to play around it a bit more to fully grasp it, but we're getting there!
Reply
#9
(2023-01-02, 14:29:06)hsacrm Wrote: Yes, it works! Thank you very much.

I've played a bit around with it. The behaviour is somewhat different than what i expected.
Am i correct when saying that the keyaccelator only work when pressing a key modifier? (Ctrl, alt etc)
The way it activates may bring some difficulties, to catch ctrl + s for example. The function gets called allot of times when holding down the ctrl-key (which even lead to a crash once).
We can reduce it by checking if the key is released.  It will then get called twice. Once for the ctrl-key and once for the S-key. So i think we have to build something to wait till the key get released and check what was pressed in the meantime.

We have to play around it a bit more to fully grasp it, but we're getting there!

Glad to hear that you got it to work.
Yes, this interface only works for accelerator keys.
So shortcut keys with modifier keys like you say, but it also supports some special keys like Escape and function keys.

Yes, it is different from what you expect.
The reason for that is as I explained earlier here [1], let me quote.
The problem is that the WebView2 control is Out-Of-Process and has an In-Process front end.
So while you're typing in the -out of process- control, the -in process- front end doesn't get any of those events.


It would be good if MS adds a proper interface for keyboard events.
But at least you can catch short cut keys now without having to do this at the javascript layer.

--
Wil
Reply
#10
I've continued to coding around the keyevent, and it seems to work. However, one small problem is that we can't know whether the shift-key is pressed.
For example, when pressing ctrl + shift, we only get that ctrl is pressed. With ctrl + shift + g, only ctrl and g.
When i read the microsoft website it says that is should be possible with scancodes: https://learn.microsoft.com/en-us/window...sage-flags

Is there a way to return whether the shift-key is pressed as well?
Reply
#11
Hi,

The PhysicalKeyStatus parameter of type AntViewPhysicalKeyStatus returns a scancode as one of its properties.
Have you tried to get the info via that already?

--
Wil
Reply
#12
yes, but it doesn't return a different value when pressing the shift-key.
Reply
#13
You are quite right.
The data is passed as is and is what MS gives me, so the scancode is what it is.
I don't think it is a good idea if AntView changes the actual scancodes.
However, not all is lost.

Was just looking at the test code and down here, we also bumped into the same issue.

You can get the status of the modifier keys via the GetKeyState windows API call.

The nVirtKey parameter for GetKeyState is $10 for VK_SHIFT and $11 for VK_CONTROL

So not too hard to get the state of the modifier keys if you're able to access the Windows API.

--
Wil
Reply
#14
Hi,

Nevermind the GetKeyState API call.
You won't be the only one who bumps into this and as this feature is not yet public, it was super easy to quickly add the functionality.

The AntViewPhysicalKeyStatus interface now also has IsControlKeyDown and IsShiftKeyDown properties.
So that would be the easiest way for you.

I will be sending out a new private build later today.

--
Wil
Reply
#15
Hello Wil,

I've tried the latest build. It works perfectly fine. Thanks.

These aren't issues for me anymore, just wanted to tell my observation if anyone has the same problems in the future:
- As long as you press down a key, the event will be called an endless time. So make sure your code is compatible with this.
- IsShiftKeyDown and IsControlKeyDown will always return 0 if you release it.
This means you cannot do a check like `if keyreleased then` because you won't know anymore if the accelatorkeys were pressed. So when pressing or holding down ctrl + shift + x, IsControlKeyDown and IsShiftKeyDown will return 1. Releasing it will show 0 even if you check whether the x-key was pressed on release.
- You cannot debug manually in VBA, the code will simply stop executing at a breakpoint as if it's never called. Debug by using print-statements. It probably has something to do with that the browser is async.
Reply
#16
(2023-01-05, 11:00:10)hsacrm Wrote: Hello Wil,

I've tried the latest build. It works perfectly fine. Thanks.

These aren't issues for me anymore, just wanted to tell my observation if anyone has the same problems in the future:
- As long as you press down a key, the event will be called an endless time. So make sure your code is compatible with this.
- IsShiftKeyDown and IsControlKeyDown will always return 0 if you release it.
This means you cannot do a check like `if keyreleased then` because you won't know anymore if the accelatorkeys were pressed. So when pressing or holding down ctrl + shift + x, IsControlKeyDown and IsShiftKeyDown will return 1. Releasing it will show 0 even if you check whether the x-key was pressed on release.
- You cannot debug manually  in VBA, the code will simply stop executing at a breakpoint as if it's never called. Debug by using print-statements. It probably has something to do with that the browser is async.

Glad to hear it works for you.

The code was tested on key-up, not key down. Even on key-up the shift and control modifiers still showed as "down".
For IsShiftKeyDown and IsControlKeyDown I figured to use a similar interface as WebView2's CoreWebView2PhysicalKeyStatus interface. So the return data type is integer and it returns 1 instead of using booleans.

One difference though is that -since I'm using the GetKeyState API- is that the function IsShiftKeyDown/IsControlKeyDown test the moment you query them.
This means that if you would run in a debug session and set a breakpoint that the status of these keys are showing the key status from your debug session. Not the status from before the breakpoint triggers.
Perhaps this can be handled, but certainly not now as I'm very close to a release and don't want to risk breaking things.
I have updated the documentation to mention that tidbit as it is an important difference.
--
Wil
Reply
#17
(2023-01-05, 11:39:41)wila Wrote: ..
The code was tested on key-up, not key down. Even on key-up the shift and control modifiers still showed as "down".
..

Are you sure you meant keyup? Maybe the browser returns them in a reversed order, because here are my results by pressing only the ctrl-key:

On down:
keyevent 0
virtualkey 17
KeyEventLParam 1900545
KeyEventKind 0
IsExtendedKey 0
IsKeyReleased 0
IsMenuKeyDown 0
RepeatCount 1
ScanCode 29
WasKeyDown 0
IsShiftKeyDown 0
IsControlKeyDown 1

On release:
keyevent 1
virtualkey 17
KeyEventLParam -1071841279
KeyEventKind 1
IsExtendedKey 0
IsKeyReleased 1
IsMenuKeyDown 0
RepeatCount 1
ScanCode 29
WasKeyDown 1
IsShiftKeyDown 0
IsControlKeyDown 0
Reply
#18
Hi,

Yes.. and I understand what the difference is.. I tested with Ctrl+T and on releasing the "T" it still shows the Ctrl+Key as down.. which makes sense Big Grin

Either way.. it works.
--
Wil
Reply
#19
Ah yes, you're quite right. I was releasing both keys at the same time, that's why it didn't show as pressed.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)