Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shift key not recognized when focus is changed to QB64 program
#11
My keyboard library works fine with it in windows.  It's all incorporated into my toolkit now.

https://github.com/SteveMcNeill/QB64-Pho...on-Toolbox
Reply
#12
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.
Reply
#13
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.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#14
(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
Reply
#15
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
Fake News + Phony Politicians = Real Problems

Reply
#16
(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.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#17
@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
Fake News + Phony Politicians = Real Problems

Reply
#18
(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
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply




Users browsing this thread: 2 Guest(s)