Page 1 of 1

Where are my Function Keys?

Posted: Thu Feb 05, 2015 5:08 pm
by RichardL
Hi,
Long story short... I'm running two programs: (1) a Logging pProgram, which may be third party (ie: not mine), and (2) my Control Program which sends stuff in and out of the serial port when one of four buttons are clicked and manages lots of audio stuff. All working fine so far.

Users would like to trigger one of the four buttons on program (2) without clicking on it to transfer the focus away from program (1), clicking the wanted button and then clicking back on program (1). This is entirely reasonable because it saves time when they don't have any to waste and is less error prone under stress.

I modified program (2) to monitor four function keys, with a neat little editor to define four keys not in use by program (1) thus avoiding clashes. It works. Mostly. Some of the functions keys NOT used by program (1) can be monitored by program (2) which is parked out of the way; BUT some function keys seem to remain inactive and look like they are robbed by Windoze.

Even if I compile the following code as an executable and kill jaPBe and ThunderBird (normally running all the time) I still have missing keys. Does anyone know (A) how to determine who owns a function key at any time? (B) How to monitor my choice of function keys, assuming I have dodged keys assigned by launched programs?

Code: Select all

OpenWindow(1,100,100,100,100,"FKeyTest")
StartDrawing(WindowOutput(1))
Repeat


  If GetAsyncKeyState_(#VK_F1) : DrawText(10,10, "F1  ") : While GetAsyncKeyState_(#VK_F1)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F2) : DrawText(10,10, "F2  ") : While GetAsyncKeyState_(#VK_F2)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F3) : DrawText(10,10, "F3  ") : While GetAsyncKeyState_(#VK_F3)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F4) : DrawText(10,10, "F4  ") : While GetAsyncKeyState_(#VK_F4)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F5) : DrawText(10,10, "F5  ") : While GetAsyncKeyState_(#VK_F5)  : Wend : EndIf
  
  If GetAsyncKeyState_(#VK_F6) : DrawText(10,10, "F6  ") : While GetAsyncKeyState_(#VK_F6)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F7) : DrawText(10,10, "F7  ") : While GetAsyncKeyState_(#VK_F7)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F8) : DrawText(10,10, "F8  ") : While GetAsyncKeyState_(#VK_F8)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F9) : DrawText(10,10, "F9  ") : While GetAsyncKeyState_(#VK_F9)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F10): DrawText(10,10, "F10 ") : While GetAsyncKeyState_(#VK_F10) : Wend : EndIf

  If GetAsyncKeyState_(#VK_F11): DrawText(10,10, "F11 ") : While GetAsyncKeyState_(#VK_F11) : Wend : EndIf
  If GetAsyncKeyState_(#VK_F12): DrawText(10,10, "F12 ") : While GetAsyncKeyState_(#VK_F12) : Wend : EndIf
  
  If GetAsyncKeyState_(#VK_ESCAPE) : Break : EndIf
  
  WaitWindowEvent(10)
 
ForEver
Thanks in advance
RichardL

Edit: @VB6_to_PBx ... Fixed F8 typo... thanks for pointing it out :oops:

Re: Where are my Function Keys?

Posted: Thu Feb 05, 2015 6:12 pm
by RASHAD
Hi Richard
Any special reason to use GetAsyncKeyState_() ?

Code: Select all

OpenWindow(1,0,0,100,100,"Test",#PB_Window_ScreenCentered) 
StartDrawing(WindowOutput(1))
Repeat
  Select WaitWindowEvent() 
      Case #WM_KEYDOWN,#WM_SYSKEYDOWN 
            Box(10,10, 28,18,#Black)
            Text$ = "F"+Str(EventwParam() - 111)         
            DrawText(10,10, Text$)
  EndSelect 
ForEver

Re: Where are my Function Keys?

Posted: Thu Feb 05, 2015 6:12 pm
by VB6_to_PBx
you have 2 "If GetAsyncKeyState_(#VK_F8)"
one correct , and this one wrong => If GetAsyncKeyState_(#VK_F8) : DrawText(10,10, "F9 ") : While GetAsyncKeyState_(#VK_F9) : Wend : EndIf

Code: Select all

  If GetAsyncKeyState_(#VK_F8) : DrawText(10,10, "F8  ") : While GetAsyncKeyState_(#VK_F8)  : Wend : EndIf
  If GetAsyncKeyState_(#VK_F8) : DrawText(10,10, "F9  ") : While GetAsyncKeyState_(#VK_F9)  : Wend : EndIf

Re: Where are my Function Keys?

Posted: Thu Feb 05, 2015 6:41 pm
by VB6_to_PBx
F10 seems to lockup my F1 to F9 and F11 to F12 Keys on my Keyboard
until F10 is pressed again , then Functions Keys are free to work again

but anytime F10 is pressed , it will lockup other Function Keys until F10 is pressed once more to release the other Function Keys

Re: Where are my Function Keys?

Posted: Fri Feb 06, 2015 1:12 am
by PB Fanatic
If you want to check if a key is down, you have to use GetAsyncKeyState_(#VK_F1) & $8000, not just GetAsyncKeyState_(#VK_F1) alone. The $8000 is needed.

Re: Where are my Function Keys?

Posted: Fri Feb 06, 2015 8:51 am
by VB6_to_PBx
PB Fanatic wrote:If you want to check if a key is down, you have to use GetAsyncKeyState_(#VK_F1) & $8000, not just GetAsyncKeyState_(#VK_F1) alone. The $8000 is needed.
did not seem to make any difference to the problem with the F10 Function Key :(
i tested it on two different Computers w/ different OS ... same results with the F10 Key

i wonder if RichardL's problem includes that F10 Key problem
along with the mixup on this Code Line : ??

Code: Select all

If GetAsyncKeyState_(#VK_F8) : DrawText(10,10, "F9  ") : While GetAsyncKeyState_(#VK_F9)  : Wend : EndIf

RASHAD's solution works with all the Function Keys , especially the F10 Key
RASHAD wrote:Hi Richard
Any special reason to use GetAsyncKeyState_() ?

Code: Select all

OpenWindow(1,0,0,100,100,"Test",#PB_Window_ScreenCentered) 
StartDrawing(WindowOutput(1))
Repeat
  Select WaitWindowEvent() 
      Case #WM_KEYDOWN,#WM_SYSKEYDOWN 
            Box(10,10, 28,18,#Black)
            Text$ = "F"+Str(EventwParam() - 111)         
            DrawText(10,10, Text$)
  EndSelect 
ForEver
anyone knows why theres a problem with the F10 Key ??

Re: Where are my Function Keys?

Posted: Fri Feb 06, 2015 11:11 am
by RichardL
Hi Guys,
Thanks for the comments. Sorry for the 'F8' typo in the first posting (fixed), but it still leaves a problem.

I have looked at MS docs for the keyboard functions and they are IMHO a model of obfuscation, unless you already know what they mean. I'm none the wiser except it seems that mysterious things can come out of the woods and do unexpected things. This is not a safe way for me to issue a program to users.

I made a simpler test utility that examines keys F9 to F12... my preferred group because some logging programs use the lower keys.
On my desktop machine F10 behaves as a two state switch that toggles F9, F11 and F12 on/off.

Code: Select all

OpenWindow(1,100,100,100,100,"FKeyTest")
StartDrawing(WindowOutput(1))
Repeat
  T =  GetAsyncKeyState_(#VK_F9) : If T <> 0 : beep_(2500,55) : EndIf
  T =  GetAsyncKeyState_(#VK_F10): If T <> 0 : beep_(2500,55) : EndIf
  T =  GetAsyncKeyState_(#VK_F11): If T <> 0 : beep_(2500,55) : EndIf
  T =  GetAsyncKeyState_(#VK_F12): If T <> 0 : beep_(2500,55) : EndIf
  
  If GetAsyncKeyState_(#VK_ESCAPE) : Break : EndIf
  
 WaitWindowEvent(10)
 
Forever
What looked like a neat and simple solution appears not to be!

@RASHAD
I think your solution relies on the program monitoring the keyboard having the focus. I'm trying to monitor keys when another program has the focus.

RichardL

Re: Where are my Function Keys?

Posted: Sat Feb 07, 2015 2:08 am
by Demivec
RichardL wrote:I made a simpler test utility that examines keys F9 to F12... my preferred group because some logging programs use the lower keys.
On my desktop machine F10 behaves as a two state switch that toggles F9, F11 and F12 on/off.
According to this reference, F10 activates the 'menu bar'. I think this is equivalent to simply pressing 'Alt'. I wasn't familiar with this function and so I tested it in my browser while I was typing this response and it did indeed activate the menu bar in my program.

This means that when you activate the menu bar it then takes your key presses to navigate the menu or select menu items. You would deactivate the menu without making a selection by pressing F10 or 'Alt' again. This would explain the behavior you witnessed.

Re: Where are my Function Keys?

Posted: Sat Feb 07, 2015 11:59 am
by luis
Oh yes, that's it.

There are various quirks to be considered with some keys generating different WK_KEYDOWN you would not expect.

I had to wrestle with them myself in the past, here are somewhat related useful info if you want to dig into them

http://www.purebasic.fr/english/viewtop ... 17#p330617

The link in the first post is very informative and the the trick I used later on involving the messages timestamp is pure gold IMO.

Re: Where are my Function Keys?

Posted: Tue Feb 10, 2015 11:56 am
by Thorium
VB6_to_PBx wrote:
PB Fanatic wrote:If you want to check if a key is down, you have to use GetAsyncKeyState_(#VK_F1) & $8000, not just GetAsyncKeyState_(#VK_F1) alone. The $8000 is needed.
did not seem to make any difference to the problem with the F10 Function Key :(
i tested it on two different Computers w/ different OS ... same results with the F10 Key
It's still the right thing to do. It might not fix the problem you describe but it fixes the problem of you code sometimes dont trigger if a key is down.

Masking the return value is not optional.
The return value does not only tell if it's currently down but also if it was down since the last call. And thats why you need to use a bitmask.
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx