[Solved] - Program hangs on MultiKey()?

General FreeBASIC programming questions.
Post Reply
cbruce
Posts: 163
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

[Solved] - Program hangs on MultiKey()?

Post by cbruce »

.
Program hangs forever when trying to get user input of [ESC] or [Ctrl-C] with MultiKey().

Windows 10 64-bit
FreeBASIC-1.07.1-gcc-8.4 (same hang with/without gcc optimizations)

Code: Select all

#include once "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB '' Keyboard scan code constants are stored in the FB namespace in lang FB
#endif
'
type TESTDATA__type
  A as ulongint
  B as ulongint
  C as ulongint
end type
dim shared as TESTDATA__type  gTestData
' ------------------------------------------------------------
sub Wait_For_ESC_or_CtrlC__Then_Return()
  ' Clear Inkey buffer
  While Inkey <> "": Wend
  Do
    Sleep 25
    If MultiKey(SC_ESCAPE) Then          ' [ESC] key?
      exit do
    end if
    If MultiKey(SC_CONTROL) And MultiKey(SC_C) Then       ' [CTRL+C] key combination?
      exit do
    end if
  Loop
end sub
' ------------------------------------------------------------
sub Wait_For_ESC__Then_Return()
  ' Clear Inkey buffer
  While Inkey <> "": Wend
  Do
    Sleep 25
  loop until inkey = chr(27)          ' [ESC] key?
end sub
' ------------------------------------------------------------
Function read_testdata_file() as integer
  dim as integer f
  f = freefile()
  open "TESTDATA.DAT" for input as #f
  If Err > 0 Then
    ' << You folks shouldn't have a TESTDATA.DAT file, so you should be in this error handler >>
    Print "Error opening TESTDATA.DAT"
    Wait_For_ESC_or_CtrlC__Then_Return()    '' << MultiKey() HANGS >>
    '''Wait_For_ESC__Then_Return()    " << InKey() WORKS >>
    print "inside - after the Open ERR wait()"
    close #f
    return 1
  end if
  print "inside - after good file open()"
  '
  dim as string keys(1 to 3)
  input #f, keys(1), _
            keys(2), _
            keys(3)
  If Err > 0 Then
    Print "Error reading TESTDATA.DAT"
    Wait_For_ESC_or_CtrlC__Then_Return()
    '''Wait_For_ESC__Then_Return()
    print "inside - after the Read ERR wait()"
    close #f
    return 2
  end if
  print "inside - after good file read()"
  '
  close #f
  '
  gTestData.A  = val(keys(1))
  gTestData.B  = val(keys(2))
  gTestData.C  = val(keys(3))
  '
  RETURN 0
end Function
' ============================================================
print "main - before the testdata file read()"
read_testdata_file()
print "main - after the testdata file read()"
'
end
Thanks!
CBruce
Last edited by cbruce on Apr 11, 2021 1:13, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Program hangs on MultiKey()?

Post by MrSwiss »

Seems to me, that you're not looking at the correct place ... it doesn't hang, it quits!

The real problem is: you are trying to Open (for input) a non-existing file which throws a 'runtime error'.

Remedy: check first if the file exists, before calling Open "filename" for Input As #f

Code: Select all

#Include "file.bi"
' contains: FileExists("filename") As Long, function
cbruce
Posts: 163
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

Re: Program hangs on MultiKey()?

Post by cbruce »

Thanks for the use of FileExists(), but I still have the MultiKey() question.

If you put a couple of PRINTs in, you will find that the code stub gets into the the IF ERR code, calls Wait_For_ESC_or_CtrlC__Then_Return(), and then sits there comfortably forever.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Program hangs on MultiKey()?

Post by MrSwiss »

Have you updated WinFBE yet?

Before that, guessing is pointless ...
cbruce
Posts: 163
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

Re: Program hangs on MultiKey()?

Post by cbruce »

Hmm... curious, why would a WinFBE update matter?

Anyway, I am on the latest build of WinFBE - and I have tried it with both gcc 5.2 and 8.4, with and without optimizations.

Once MultiKey(SC_ESC), etc. is called - the program just hangs. I can only get out of it with a Ctrl-Break.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Program hangs on MultiKey()?

Post by MrSwiss »

cbruce wrote:Hmm... curious, why would a WinFBE update matter?
GCC 5.2 downgrade, perhaps?
Earlier releases used GCC 8.n versions (blame deltarho[...] if you must blame someone).

Try below simplified test-code ... seems OK to me:

Code: Select all

#Include "fbgfx.bi"
Using FB

Sub Wait_For_ESC_or_CtrlC__Then_Return()
  Do
    Sleep 25
    If MultiKey(SC_ESCAPE) Then Exit Do          ' [ESC] key?
    If MultiKey(SC_F4) Then Exit Do
  Loop
end Sub


Dim As Long counter = 0
Dim As ZString * 3 key

Do
    ' Clear Inkey buffer
    While Inkey <> "": Wend
    counter += 1
    Wait_For_ESC_or_CtrlC__Then_Return
    key = InKey
    Print counter & "th call"
    Sleep(100, 1)
Loop Until key = Chr(255, 107)  ' [Alt] + [F4]
Last edited by MrSwiss on Apr 11, 2021 0:39, edited 1 time in total.
cbruce
Posts: 163
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

Re: Program hangs on MultiKey()?

Post by cbruce »

@MrSwiss, does the stub compile and run for you? Does it get the file error and get to the sub that calls MultiKey()? And does it hang the program then for you?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Program hangs on MultiKey()?

Post by MrSwiss »

Remark: [Ctrl] + [c] can't be used in console mode (quit's console & program).

My code uses SC_ESC + SC_F4 for the Sub ... [Alt] + [F4] to quit main-loop.
cbruce
Posts: 163
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

[Solved] = Program hangs on MultiKey()?

Post by cbruce »

For years, I have run a portable app called Console that let's me run multiple tabbed console windows at the same time.

I just went and ran my code in a straight Windows CMD.exe console and MultiKey() runs as expected.

Something in Console Portable is grabbing the keyboard input and not letting the keycodes get to the MultiKey() function.

Thanks!
Post Reply