basicqbasicqb64

Trap Alt-F4 in QB64


I have a utility that has a function called by Alt-F4 but I can't because it closes the window. How can I trap Alt-F4 in QB64?

The following code samples shows various ways to check for Alt-F4:

REM Trap Alt-F4 in QB64
DO
    _LIMIT 100
    x$ = INKEY$
    IF x$ = CHR$(27) THEN END
    IF LEN(x$) = 2 THEN
        IF ASC(RIGHT$(x$, 1)) = 107 THEN
            PRINT "Alt-F4"
        END IF
    END IF
LOOP
END

Also tried this with no effect:

KEY 15, CHR$(8) + CHR$(62)
ON KEY(15) GOSUB KeyTrap
KEY(15) ON
DO
    _LIMIT 100
    x$ = INKEY$
    IF x$ = CHR$(27) THEN END
LOOP
END
KeyTrap:
PRINT "Alt-F4"
RETURN

Also tried to trap Alt-F4 in _EXIT trap with no results:

' start timer trap.
ON TIMER(1) GOSUB Timer.Trap
TIMER ON
VarQ = _EXIT
DO
    _LIMIT 100
LOOP
END
' timer ctrl-break trap.
Timer.Trap:
VarQ = _EXIT
_DELAY 1
IF VarQ THEN ' Control-Break
    x$ = INKEY$
    IF LEN(x$) = 2 THEN
        IF ASC(RIGHT$(x$, 1)) = 107 THEN
            PRINT "Alt-F4"
            RETURN
        END IF
    END IF
    CLS
    SYSTEM
END IF
RETURN

Another possible way to trap Alt-F4:

Only this checks more keyhits.

CONST KEY_RALT& = 100307
CONST KEY_LALT& = 100308
' start timer trap.
ON TIMER(1) GOSUB Timer.Trap
TIMER ON
VarQ = _EXIT
DO
    _LIMIT 100
LOOP
END
' timer ctrl-break trap.
Timer.Trap:
VarQ = _EXIT
_DELAY 1
IF VarQ THEN ' Control-Break
    x = _KEYHIT
    IF x = CVI(CHR$(0) + CHR$(62)) THEN ' F4
        IF _KEYDOWN(KEY_RALT&) OR _KEYDOWN(KEY_LALT&) THEN
            PRINT "Alt-F4"
            RETURN
        END IF
    END IF
    CLS
    SYSTEM
END IF
RETURN

Also have a sample code for checking using memory addresses:

As well, it does not either trap Alt-F4:

DO
    _LIMIT 30
    b$ = INKEY$
    IF LEN(b$) THEN
        IF b$ = CHR$(27) THEN END
        DEF SEG = 0
        IF PEEK(1047) MOD 16 = 8 THEN
            IF b$ = CHR$(0) + "k" THEN
                PRINT "Alt-F4"
            END IF
        END IF
        DEF SEG
    END IF
LOOP

Solution

  • I believe the answer is very simple:

    Run in full screen.

    Alt+Enter or alter your program settings. I forget what the chr$ codes for Alt+F4 is, but I'm pretty sure it was a CHR$(0) + CHR$(x) format.

    Hope this helps!