Programs (.BAS), Tutorials and Libraries ($INCLUDE, DECLARE LIBRARY, ...) > Sample Programs
Use Win API to monitor keystate even when minimized (cheapo hotkey)
Dav:
Couldn't get/set Hotkeys in a QB64 program yet, so resorted to this method which seems to work. This example will maximize a minimized program window when Shift+A is pressed.
- Dav
EDIT: GetForegroundWindow%& and SetForegroundWindow& functions added by Clippy to return focus back to program when maximized! Thanks Clippy.
--- Code: ---'=================
'CHEAPO-HOTKEY.BAS
'=================
'Uses GetKeyState Win API to monitor a Key state.
'This demo will maximize the window and focus on program when Shift+A is pressed.
DECLARE DYNAMIC LIBRARY "user32"
FUNCTION FindWindowA%& (BYVAL ClassName AS _OFFSET, WindowName$) 'find process handle by title
FUNCTION GetKeyState% (BYVAL nVirtKey AS LONG) 'Windows virtual key presses
FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG) 'maximize process
FUNCTION GetForegroundWindow%& 'find currently focused process handle
FUNCTION SetForegroundWindow& (BYVAL hwnd AS _OFFSET) 'set foreground window process(focus)
END DECLARE
title$ = "Cheapo Hotkey (Shift+A)" 'title of program window
_TITLE title$ 'set program title
hwnd%& = FindWindowA(0, title$ + CHR$(0)) 'find this program's process handle
PRINT "Minimize this window, then Press Shift+A to bring it back up."
'=== below minimizes it for you
_DELAY 4
x& = ShowWindow&(hwnd%&, 2)
'===========================
DO
IF GetKeyState(16) < 0 AND GetKeyState(ASC("A")) < 0 THEN '<==== Shift+A
FGwin%& = GetForegroundWindow%& 'get current process in focus
PRINT hwnd%&; FGwin%&
x& = ShowWindow&(hwnd%&, 1) 'maximize minimized program
IF FGwin%& <> hwnd%& THEN y& = SetForegroundWindow&(hwnd%&) 'set focus when necessary
PRINT "That is all. Hoped it worked."; x&; y&
_DELAY 5: END 'delay allows user to not minimize the window
END IF
_LIMIT 30 'save CPU usage while waiting for key press
LOOP
--- End code ---
Clippy:
Is GetKeyState% a Windows function? Do you have a list of key codes for function keys?
The window comes back up, but it is not in focus. I wish we had a focus function...
Galleon:
A quick (but not very elegant) platform independent way to achieve this is to monitor when CAPS LOCK is toggled which can be achieved when your app isn't in focus.
Dav:
Clippy: It's a user32 function, which I didn't specify in the declare. I've corrected the DECLARE LIBRARY in the code. The key values on my keyboard (US) are these (I don't know about Non-US):
F1=112, F2=113, on upward to ... F12=123
Any SHIFT = 16, LShift = 160, RShift = 161
Any CTRL = 17, LCtrl = 162, RCtrl = 163
Any ALT = 18, LAlt=164, RAlt=165
Left=37, Up=38, Right=39, Down=40
Tab=9, TAB=20,PgUp=33, PgDn=34, End=35, Home=36
You can use the code below to find more. Here's the GetKeyState info I found.
- Dav
--- Code: ---DO
FOR t = 0 TO 255
IF GetKeyState(t) < 0 THEN
LOCATE 1, 1: PRINT t
END IF
NEXT
LOOP UNTIL GetKeyState(27) < 0 '<=== ESC exits
--- End code ---
Clippy:
I tried to get the focus, but no luck:
--- Code: ---'=================
'CHEAPO-HOTKEY.BAS
'=================
'Uses GetKeyState Win API to monitor a Key state.
'This demo will maximize the window when Shift+A is pressed.
DECLARE DYNAMIC LIBRARY "user32"
FUNCTION GetKeyState% (BYVAL nVirtKey AS LONG)
FUNCTION BringWindowToTop& (BYVAL hwnd AS _OFFSET)
END DECLARE
DECLARE CUSTOMTYPE LIBRARY
FUNCTION FindWindow& (BYVAL ClassName AS _OFFSET, WindowName$)
FUNCTION ShowWindow& (BYVAL hwnd AS _OFFSET, BYVAL nCmdShow AS LONG)
END DECLARE
_TITLE "Cheapo Hotkey (Shift+A)"
hwnd%& = FindWindow(0, "Cheapo Hotkey (Shift+A)" + CHR$(0))
PRINT "Minimize this window, then Press Shift+A to bring it back up."
'=== below minimizes it for you
'x = ShowWindow&(hwnd, 2)
'===========================
DO
IF GetKeyState(16) < 0 AND GetKeyState(ASC("A")) < 0 THEN '<==== Shift+A
x& = ShowWindow&(hwnd%&, 1)
y& = BringWindowToTop&(hwnd%&)
PRINT "That is all. Hoped it worked."; x&; y&
END
END IF
_LIMIT 30 'Don't be a hog
LOOP
--- End code ---
Any ideas? http://qb64.net/wiki/index.php?title=Windows_Libraries#Hot_Keys_.28maximize.29
This is where a _MIDDLE function might be used to _SCREENCLICK to get focus...
Navigation
[0] Message Index
[#] Next page
Go to full version