Recent

Author Topic: a console program using mouse unit, but no reaction when click mouse  (Read 1001 times)

Laoli68

  • Newbie
  • Posts: 6
I'm learning the MOUSE unit for console programming, the following code is from the RTL document. When i ran it and click mosue button,there was no reaction. My environemnt: Window 10, fpc 3.2.2

uses mouse;

var
  event:TMouseEvent;
  visible:boolean;
begin
  InitMouse;
  ShowMouse;
  Visible:=True;
  writeln('Press left mouse button to hide/show, right button quits');
  repeat
    getMouseEvent(event);
    with event do
      if (buttons=MouseLeftButton) and (Action=MouseActionDown) then
      begin
        if Visible then
          HideMouse
        else
          ShowMouse;
      end;
  until (event.Buttons=MouseRightButton) and (event.Action=MouseActionDown);
  DoneMouse;
end.
     

Thaddy

  • Hero Member
  • *****
  • Posts: 19609
  • Glad to be alive.
Re: a console program using mouse unit, but no reaction when click mouse
« Reply #1 on: August 12, 2025, 05:13:12 pm »
There should be an action, but not YOUR action on right-click: the system has captured that mouse event for pasting.
Also, your event object is not initialized. When it is, the behavior should be correct.
Any "programmer" that knows only one programming language is not a programmer

mika

  • Full Member
  • ***
  • Posts: 139
Re: a console program using mouse unit, but no reaction when click mouse
« Reply #2 on: August 12, 2025, 06:43:52 pm »
This is example mouse and keyboard unit. Under Linux mouse unit does not work without keyboard unit.

Code: Pascal  [Select][+][-]
  1. program testKeyboardMouse;
  2.  
  3. uses keyboard, mouse;
  4. var ke : char;
  5.     Event, chEv : TKeyEvent;
  6.     i : word;
  7.     keyCode : word;
  8.     CharCode : word;
  9.     ScanCode : word;
  10.     keyShift : word;
  11.     flag : byte;
  12.     ch : char;
  13.     ShEvent : TKeyEvent;
  14.     MEvent : TMouseEvent;
  15. begin
  16.      writeln('Keyboard and mouse unit test (To quit press Esc).');
  17.      initkeyboard;
  18.      initmouse;
  19.      ShowMouse;
  20.      repeat
  21.           if PollMouseEvent(MEvent) then
  22.           begin
  23.             GetMouseEvent(MEvent);
  24.             with MEvent do
  25.             begin
  26.               HideMouse;
  27.               writeln(x:4,y:4, binstr(buttons,16):18,'  action ', Action);
  28.               ShowMouse;
  29.             end;
  30.           end;
  31.  
  32.           Event := PollKeyEvent;
  33.           if Event <> 0 then Event := GetKeyEvent;
  34.  
  35.           if Event <> 0 then
  36.           begin
  37.  
  38.                shEvent:=PollShiftStateEvent;
  39.  
  40.                KeyShift:=GetKeyEventShiftState(Event);
  41.                keyCode:=GetKeyEventCode(Event);
  42.                ScanCode:=keyCode shr 8;
  43.                charCode:=keyCode and $ff;
  44.                chEv:=TranslateKeyEvent(Event);
  45.                ch:=GetKeyEventChar(chEv);
  46.  
  47.                flag:=GetKeyEventFlags(Event);
  48.  
  49.                HideMouse;
  50.                write(i:2);
  51.                write(' KeyCode ' ,hexstr(KeyCode,4));
  52.  
  53.                write(' CharCode ',hexstr(CharCode,2));
  54.                write(' ScanCode ',hexstr(ScanCode,2));
  55.  
  56.                write(' KeyShift ',hexstr(KeyShift,2));
  57.  
  58.                write(' KeyEv ',hexstr(Event,8));
  59.                write(' F ',hexstr(flag,2));
  60.  
  61.                if ch > #31 then write(' ',ch);
  62.                if shEvent <> 0 then
  63.                write(' shKeyEv ',hexstr(shEvent,8));
  64.                writeln {(keyEventToString(Event))};
  65.                ShowMouse;
  66.  
  67.                inc(i);
  68.                //if i > 20 then break;
  69.                ke:=char(charCode);
  70.           end;
  71.      until ke = #27;
  72.      donemouse;
  73.      donekeyboard;
  74. end.

tetrastes

  • Hero Member
  • *****
  • Posts: 774
Re: a console program using mouse unit, but no reaction when click mouse
« Reply #3 on: August 13, 2025, 01:47:14 pm »
This is example mouse and keyboard unit. Under Linux mouse unit does not work without keyboard unit.

It works in "true" text-mode console with gpm.

mika

  • Full Member
  • ***
  • Posts: 139
Re: a console program using mouse unit, but no reaction when click mouse
« Reply #4 on: August 13, 2025, 02:27:42 pm »
This is example mouse and keyboard unit. Under Linux mouse unit does not work without keyboard unit.

It works in "true" text-mode console with gpm.
True. I was saying that because that's a possible future for Windows. It has no "true" text-mode console with gpm to "relay on". Gpm itself is abandoned project. Last update 5 years ago. For example mouse wheel is not supported and it wont be ever supported by gpm.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12984
  • FPC developer.
Re: a console program using mouse unit, but no reaction when click mouse
« Reply #5 on: August 13, 2025, 02:40:20 pm »
In the past mouse support was default, but in later versions Windows switched the default to copy-and-paste.

See https://forum.lazarus.freepascal.org/index.php/topic,68264.msg527315.html#msg527315

 

TinyPortal © 2005-2018