QB64 Phoenix Edition
Shift key not recognized when focus is changed to QB64 program - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Shift key not recognized when focus is changed to QB64 program (/showthread.php?tid=3037)

Pages: 1 2


RE: Shift key not recognized when focus is changed to QB64 program - SMcNeill - 09-17-2024

My keyboard library works fine with it in windows.  It's all incorporated into my toolkit now.

https://github.com/SteveMcNeill/QB64-Phoenix-Edition-Toolbox


RE: Shift key not recognized when focus is changed to QB64 program - DSMan195276 - 09-17-2024

Something to note, on Windows the Shift keys are special/bugged, I think that's part of what you're seeing and probably why _KeyDown doesn't report it in this case. I put in a bug fix for the shift keys a while back that applies to _Devices, that's why it's always correct, in that case it uses GetAsyncKeyState to check the shift keys rather than relying on the WM_KEYDOWN and WM_KEYUP messages.

In regards to keyboard state when not in focus, in theory I don't think we should see any keys when the window loses focus but they should appear as pressed when the window regains focus if they were held down. GetAsyncKeyState does allow you to see the keys at all times on Windows, I believe. On Linux and potentially Mac OS that's not always allowed for security reasons though, so it's not really something you can reliably implement in a platform-independent way.


RE: Shift key not recognized when focus is changed to QB64 program - TerryRitchie - 09-17-2024

Well _BUTTON does work as I showed previously so there is a way to work with it. I'm assuming Steve's library uses _BUTTON as well. That's also what I used for my controller library which picks up the keys just fine too.


RE: Shift key not recognized when focus is changed to QB64 program - SMcNeill - 09-17-2024

(09-17-2024, 08:59 PM)TerryRitchie Wrote: Well _BUTTON does work as I showed previously so there is a way to work with it. I'm assuming Steve's library uses _BUTTON as well. That's also what I used for my controller library which picks up the keys just fine too.

What I wrote predates the _DEVICES and _BUTTON fixes, so it relies on the Windows APIs that Matt mentioned in the post before yours.  That's why it's a Windows-Only expansion to _KeyHit, and doesn't work on all platforms.  Wink


RE: Shift key not recognized when focus is changed to QB64 program - Pete - 09-17-2024

Same here, for my multi-windows programs where I use Windows products to communicate with an open QB64 window, I always use a Win32 API method I coded for mouse and squirrel, I mean keyboard.

Pete Wink


RE: Shift key not recognized when focus is changed to QB64 program - TerryRitchie - 09-18-2024

(09-17-2024, 09:54 PM)SMcNeill Wrote:
(09-17-2024, 08:59 PM)TerryRitchie Wrote: Well _BUTTON does work as I showed previously so there is a way to work with it. I'm assuming Steve's library uses _BUTTON as well. That's also what I used for my controller library which picks up the keys just fine too.

What I wrote predates the _DEVICES and _BUTTON fixes, so it relies on the Windows APIs that Matt mentioned in the post before yours.  That's why it's a Windows-Only expansion to _KeyHit, and doesn't work on all platforms.  Wink
Oh, I'll need to take a look at that. I played with a few API calls a long while back and didn't get very far with them.


RE: Shift key not recognized when focus is changed to QB64 program - Pete - 09-18-2024

@TerryRitchie

This uses your routine with Win32 added. It will detect Shift key down when your return focus to the window.

1) Run program.
2) Click your desktop to take away focus.
3) Hold a shift key down.
4) Click your QB64 window to return focus while still holding down the shift key.

Code: (Select All)
Declare Dynamic Library "user32"
    Function GetAsyncKeyState% (ByVal vkey As Long)
End Declare
Locate 8, 2
Print "NOTE: _KEYDOWN only registers SHIFT keys and some of the other keyboard keys"
Print "      _KEYDOWN will register multiple keys"
Print "      A negative _KEYHIT value means the key was released"
Do
    Locate 2, 2
    If _WindowHasFocus Then
        'IF focus = 0 THEN _KEYCLEAR
        focus = -1
        Print "WINDOW HAS FOCUS          "
    Else
        focus = 0
        Print "WINDOW DOES NOT HAVE FOCUS"
    End If
    a$ = InKey$
    Locate 4, 2
    Print "INKEY$  =  "; a$
    Locate 5, 2
    Print "_KEYHIT  = ";
    k = _KeyHit
    If k <> 0 Then Print k; "    "
    Locate 6, 2
    Print "_KEYDOWN =  ";
    If _KeyDown(100304) And _KeyDown(100303) Then
        Print "LEFT & RIGHT SHIFT"
    ElseIf _KeyDown(100304) Or focus = -1 And GetAsyncKeyState(160) Then ' <--------------------
        Print "LEFT SHIFT        "
    ElseIf _KeyDown(100303) Or focus = -1 And GetAsyncKeyState(161) Then ' <--------------------
        Print "RIGHT SHIFT      "
    Else
        For k = 33 To 122
            If _KeyDown(k) Then
                Print Chr$(k); " "; ' print multiple keys
            End If
        Next k
        Print "                  "
    End If
    _Limit 60
Loop Until _KeyDown(27)

Pete


RE: Shift key not recognized when focus is changed to QB64 program - TerryRitchie - 09-18-2024

(09-18-2024, 03:09 AM)Pete Wrote: @TerryRitchie

This uses your routine with Win32 added. It will detect Shift key down when your return focus to the window.

1) Run program.
2) Click your desktop to take away focus.
3) Hold a shift key down.
4) Click your QB64 window to return focus while still holding down the shift key.


Pete
What a simple solution. Thanks Pete Smile