首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Ada中读取鼠标输入

在Ada中读取鼠标输入
EN

Stack Overflow用户
提问于 2020-12-08 22:44:48
回答 3查看 140关注 0票数 1

我在Ada中有一个从触摸屏输入读取的程序。代码是非常旧的,我没有触摸屏了。我想用从鼠标输入的读取来替换触摸屏代码。用C语言编写函数并将其导入到Ada代码中会更简单吗?下面的代码是触摸屏代码。

代码语言:javascript
运行
复制
   HIL_NAME : STRING (1.. 10) := "/dev/touch";

   procedure READ (X, Y : out INTEGER) is

      type BYTE is new INTEGER range 0 .. 255;
      for BYTE'SIZE use 8;
      package IN_IO is new SEQUENTIAL_IO (BYTE);
      use IN_IO;

      type DATA_TYPE is array (2 .. 9) of BYTE;

      HIL_FILE : IN_IO.FILE_TYPE;
      COUNT    : BYTE;
      DATA     : DATA_TYPE;

   begin
      IN_IO.OPEN (HIL_FILE, IN_FILE, HIL_NAME); -- open the touchscreen

      loop
         IN_IO.READ (HIL_FILE, COUNT); -- read the incoming record size
         -- read the incoming record
         for I in INTEGER range 2 .. BYTE'POS (COUNT) loop
            IN_IO.READ (HIL_FILE, DATA (I));
         end loop;
         -- is this a fingerdown?  overkill test.
         if ((COUNT = 9) and (DATA (6) = 2#01000010#) and (DATA (9) = 142)) then
            X := BYTE'POS (DATA (7)); -- pick out coordinates
            Y := BYTE'POS (DATA (8));
            IN_IO.CLOSE (HIL_FILE); -- close touchscreen to flush buffer
            return; -- return to caller
         end if;
      end loop;
   end READ;
EN

回答 3

Stack Overflow用户

发布于 2020-12-08 23:08:46

了解操作系统、版本、编译器、窗口管理器工具包和版本将非常有用。例如,我正在运行Debian10,使用Gnome3作为我的WM,我可以使用GTKAda工具包轻松地访问鼠标。上一次我写直接访问鼠标的代码是在DOS上,是在Modula 2中。

然而,GTKAda并不是特别容易学习……

如果您愿意使用web浏览器作为应用程序的GUI (这也有助于跨系统的可移植性……您甚至可以在PC上运行该应用程序,但通过平板电脑或手机访问它,从而获得触摸屏!)我推荐看一下www.gnoga.com提供的Gnoga。看看它的一些教程,它们应该很容易构建,并让你开始访问鼠标和简单的绘图。

编辑

在各种评论中找到了神奇的单词(Centos,ncurses) (如果有更好的答案,你可以将它们添加到问题中),你正在寻找的是一个到ncurses such as this one的Ada绑定。这个绑定是从5.8版本开始的官方ncurses源代码的一部分,所以应该已经在Centos上可用了。

然后,应该很简单地编写一个Read过程,该过程调用ncurses mouse handling package,在按下LH按钮时返回鼠标位置(缩放到8位整数或自然数,可能从控制台窗口的原点偏移),否则可能返回...无论OUT参数被初始化为什么(假设是BYTE‘’FIRST)

任务完成了。

现在我们可以看到触摸屏文件名,它是鼠标层次结构的一部分,可以更简单地查看在/dev/ /dev/上查找文档是否像@zerte建议的那样(或者在我的笔记本电脑上查找/dev/input/mouse[0|1] )……但我认为ncurses对机器的依赖性会更小。

票数 4
EN

Stack Overflow用户

发布于 2020-12-10 07:56:58

我已经用Ncurses解决了这个问题。我下载了terminal-interface-curses,并使用这些文件创建了以下过程。

代码语言:javascript
运行
复制
with Terminal_Interface.Curses;
use Terminal_Interface.Curses;

tmp2 : Event_Mask;
c    : Key_Code;
firsttime : Bollean;

procedure READ (X1 : out Column_Position;
                Y1 : Line_Position) is

begin
   tmp2 := Start_Mouse (All_Events);
   c:= Character'Pos ('?');
   Set_Raw_Mode (SwitchOn => True);
   Set_KeyPad_Mode (SwitchOn => True);
   firsttime := true;

   loop
      if not firsttime then
         if c = KeyMouse then
            declare
               event  : Mouse_Event;
               Y      : Line_Position;
               X      : Column_Position;
               Button : Mouse_Button;
               State  : Mouse_State;
            begin
               event := Get_Mouse;
               Get_Event (event, Y, X, Button, State);
               X1 := X;
               Y1 := Y;
               exit;
            end;
         end if;
      end if;

      firsttime := False;
      loop
         c := Get_Keystroke;
         exit when c /= Key_None;
      end loop;
   end loop;
   End_Mouse (tmp2);
end READ;
票数 2
EN

Stack Overflow用户

发布于 2020-12-10 03:47:55

您可以使用Linux输入子系统(由@Zerte建议)来读取鼠标。另请参阅SO上的这个问题以及一些内核文档herehere。读取鼠标的输入似乎并不难(至少在运行Raspbian GNU/Linux10的Raspberry Pi 3上是如此)。当然,您仍然需要应用适当的缩放,并且需要找出公开鼠标事件的设备(在我的示例中:/dev/input/event0)

注意:您可以通过检查sudo dmesg | grep "input:"__的输出找到该数字。如果鼠标(或其他定点设备)连接到inputX__,则此设备的事件将显示在eventX__上。

main.adb

代码语言:javascript
运行
复制
with Ada.Text_IO;
with Ada.Sequential_IO;
with Interfaces.C;

procedure Main is

   package C renames Interfaces.C;
   use type C.unsigned_short;
   use type C.int;

   --  Input event codes (linux/input-event-codes.h)

   EV_SYN      : constant := 16#00#;
   EV_KEY      : constant := 16#01#;
   EV_REL      : constant := 16#02#;
   EV_ABS      : constant := 16#03#;
   EV_MSC      : constant := 16#04#;
   
   BTN_MOUSE   : constant := 16#110#;
   BTN_LEFT    : constant := 16#110#;
   BTN_RIGHT   : constant := 16#111#;
   BTN_MIDDLE  : constant := 16#112#;

   REL_X       : constant := 16#00#;
   REL_Y       : constant := 16#01#;
   REL_WHEEL   : constant := 16#08#;
   
   -- Time value (sys/time.h)

   subtype suseconds_t is C.long;
   subtype time_t      is C.long;

   type timeval is record
      tv_sec  : time_t;
      tv_usec : suseconds_t;
   end record;
   pragma Convention (C, timeval);

   -- Input event struct (linux/input.h)

   type input_event is record
      time  : timeval;
      typ   : C.unsigned_short;
      code  : C.unsigned_short;
      value : C.int;
   end record;
   pragma Convention (C, input_event);

   --  ... and a package instantiation for sequential IO.

   package Input_Event_IO is new Ada.Sequential_IO (input_event);
   use Input_Event_IO;


   File  : File_Type;
   Event : input_event;

   --  Position of the mouse and wheel.
   X, Y, W : C.int := 0;

begin

   Open (File, In_File, "/dev/input/event0");

   --  Infinite loop, use Ctrl-C to exit.
   loop

      --  Wait for a new event.
      Read (File, Event);

      --  Process the event.
      case Event.typ is
         
         when EV_SYN =>
            Ada.Text_IO.Put_Line 
              (X'Image & "," & Y'Image & " [" & W'Image & "]");

         when EV_KEY =>            
            case Event.code is
               when BTN_LEFT => 
                  Ada.Text_IO.Put_Line ("Left button.");
               when BTN_MIDDLE => 
                  Ada.Text_IO.Put_Line ("Middle button.");
               when BTN_RIGHT => 
                  Ada.Text_IO.Put_Line ("Right button.");
               when others =>
                  null;
            end case;

         when EV_REL =>
            case Event.code is
               when REL_X =>
                  X := X + Event.value;
               when REL_Y =>
                  Y := Y + Event.value;
               when REL_WHEEL =>
                  W := W + Event.value;
               when others =>
                  null;
            end case;

         when EV_ABS =>
            case Event.code is
               when REL_X =>
                  X := Event.value;
               when REL_Y =>
                  Y := Event.value;
               when REL_WHEEL =>
                  W := Event.value;
               when others =>
                  null;
            end case;
         
         when others =>
            null;
         
      end case;

   end loop;

end Main;

output (在无头RPi 3上运行)

代码语言:javascript
运行
复制
pi@raspberrypi:~/mouse $ sudo obj/main
[...]
-85, 9 [-5]
-84, 9 [-5]
-83, 9 [-5]
Left button.
-83, 9 [-5]
Left button.
-83, 9 [-5]
Left button.
-83, 9 [-5]
Left button.
-83, 9 [-5]
Right button.
-83, 9 [-5]
Right button.
-83, 9 [-5]
Middle button.
-83, 9 [-5]
Middle button.
-83, 9 [-5]
-84, 9 [-5]
^C
pi@raspberrypi:~/mouse $ 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65201087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档