Up key in Dos console in text mode

Just starting out? Need help? Post your questions and find answers here.
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Up key in Dos console in text mode

Post by a_carignan »

Good morning,
Is there a way to make the up key do nothing in a Dos console in text mode?
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Up key in Dos console in text mode

Post by PBJim »

a_carignan wrote: Mon Mar 04, 2024 9:32 pm Good morning,
Is there a way to make the up key do nothing in a Dos console in text mode?
Sorry you didn't yet receive a reply, A_Carignan, you posted this last week and I started to reply, but never got around to sending it. The way I've implemented a console user interface, is to poll for my own keypresses and build my input string, rather than relying on Input(). The below may help, if this is something you want to do instead.

Code: Select all

  inpkey.s = Inkey()                                                    ; Returns ASCII input
  inpascii.i = Asc(inpkey.s)                                            ; ASCII value of inpkey
  keycode.b = RawKey()                                                  ; Returns keyboard code if input is not a conventional ASCII input
If I remember correctly, not all keys can be detected with Inkey(), so it was necessary to check for keycode as well.
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Up key in Dos console in text mode

Post by a_carignan »

Thank you, but what I would like to know is if we can remove the effect of the up key in a Dos console in text mode, without going through manual key management. The effect I want to get rid of is the fact that the up key causes the previously written text to be displayed. While I would like it to do nothing or at best go up one line.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Up key in Dos console in text mode

Post by PBJim »

a_carignan wrote: Sat Mar 16, 2024 8:15 pm Thank you, but what I would like to know is if we can remove the effect of the up key in a Dos console in text mode, without going through manual key management. The effect I want to get rid of is the fact that the up key causes the previously written text to be displayed. While I would like it to do nothing or at best go up one line.
If you use EnableGraphicalConsole(1) then you'll achieve what you need. The graphical console isn't always suitable, depending on one's other needs. I remember in my case, for other technical reasons, I was unable to use it. It doesn't allow text wrapping beyond the right margin, if my memory is correct.

Code: Select all

OpenConsole()
EnableGraphicalConsole(1)

Repeat
  Input()
ForEver
Nevertheless, the graphical console does offer you the benefit that the function keys aren't intercepted against your wishes by Windows. For instance F1 to F5 offer some command stack functions, F6 is a Ctrl/Z equivalent and F7 is a most bizarre visual command stack, reminiscent of the MSDOS days. They are best disabled in our applications. I spent a lot of time disabling unwanted features in the console, including Ctrl/C, F11 and window resizing.
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Up key in Dos console in text mode

Post by a_carignan »

The problem with graphics mode, the data entry can only contain one line. What I would like is to be able to write several lines and move around in my text without it being changed by the up key. Of course with as few lines of code as possible.
I have the impression that this is not really possible. Am I wrong?
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Up key in Dos console in text mode

Post by a_carignan »

Here is a minimalist example of a console input box. which fixes the problem with the up key by the way.

Code: Select all

EnableExplicit
Enumeration
  #event_enter
  #event_escape
EndEnumeration

Define Semaphore = CreateSemaphore()
Define thread
Define event
If OpenLibrary(0, "Kernel32.dll")
    Prototype GetConsoleWindow()
    ;Define GetConsoleWindow.GetConsoleWindow = GetFunction(0, "GetConsoleWindow")    
    Global GetConsoleWindow.GetConsoleWindow = GetFunction(0, "GetConsoleWindow")    
    CloseLibrary(0)
  ;Else 
    ;MessageRequester("<Kernel32.dll> non trouvé.",#PB_MessageRequester_Error) 
  EndIf
  Procedure onevent(event1)
    Shared event,Semaphore
    event=event1
    SignalSemaphore(Semaphore)
  EndProcedure
  
  Procedure resizewindowid()
    Define trect.rect,x,y,w,h
    GetWindowRect_(getconsolewindow(),@trect)
    x = trect\left
    y = trect\top
    w = trect\right - x
    h = trect\bottom - y
    ResizeWindow(0,x+w/2-WindowWidth(0)/2,y+h/2-WindowHeight(0)/2,#PB_Ignore,#PB_Ignore)
  EndProcedure
  Procedure activewindow_byhwnd(hwnd)
    SendMessage_(GetForegroundWindow_(), #WM_SYSCOMMAND, #SC_HOTKEY, hwnd)
    SetForegroundWindow_(hwnd)     
    BringWindowToTop_(hwnd)
    SendMessage_(hwnd,#WM_ACTIVATE,0,0)
  EndProcedure
  Procedure.s inputbox(defaut.s)
    Shared event,semaphore
    SetGadgetText(1,defaut)
    SendMessage_(GadgetID(1),#EM_SETSEL,0,Len(GetGadgetText(1)))
    HideWindow(0,#False)
    ;SetActiveWindow(0)
    activewindow_byhwnd(WindowID(0))
    SetActiveGadget(1)
    AddKeyboardShortcut(0,#PB_Shortcut_Return,#event_enter)
    AddKeyboardShortcut(0,#PB_Shortcut_Escape,#event_escape)
    WaitSemaphore(Semaphore)
    RemoveKeyboardShortcut(0,#PB_Shortcut_All)
    HideWindow(0,#True)
    activewindow_byhwnd(GetConsoleWindow())
    If event=#event_enter
      ProcedureReturn GetGadgetText(1)
    Else
      ProcedureReturn defaut
    EndIf
  EndProcedure
    
  Procedure execute(*valeur)
    Define defaut.s
    Repeat
      Print("Default value? ")
      defaut=inputbox(Input())
      PrintN(defaut)
    Until defaut="quit"
  EndProcedure
  
  

  OpenConsole()
    If OpenWindow(0, 0, 0, 220, 100, "Exemple...", #PB_Window_BorderLess|#PB_Window_WindowCentered,getconsolewindow())
      ;ButtonGadget  (1, 10, 60, 200, 30, "Fermer")
      EditorGadget(1, 10, 10, 200, 80,#PB_Editor_WordWrap)
      activewindow_byhwnd(WindowID(0))
      SetActiveGadget(1)
      HideWindow(0,#True)
      resizewindowid()
      AddWindowTimer(0,0,1)
      thread=CreateThread(@execute(),0)
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_Gadget
            Select EventGadget()
              Case 1 
                ;CloseWindow(0)
                ;End  
            EndSelect
          Case #PB_Event_Timer
            Select EventTimer()
              Case 0
                resizewindowid()
                If Not IsThread(thread)
                  Break
                EndIf
            EndSelect
          Case #PB_Event_Menu
            Select EventMenu()
              Case #event_enter
                onevent(#event_enter)
              Case #event_escape
                onevent(#event_escape)
            EndSelect
          Case #PB_Event_CloseWindow
            Break
        EndSelect
      ForEver
    EndIf   
    
Post Reply