0

I am trying to request input from a user in the mac os terminal. My program is written in c and I would like to have clickable text which allows the user to click either yes or no to let my c program execute or not execute a certain action/function.

I would like to for example see something like:

Would you like to verify your email? [Yes] [No]

Then I shall be able to either click yes or no (so basically yes/no is text acting like buttons).

I didn't find anything that answered my question whether this is possible in c. Maybe there is some kind of mac os specific function implemented which I could call from within my c program.

Thank you so much for all your help!

  • Welcome user10949666. The question is not about C. Please keep in mind that on approximately all computers all code goes down to C or equal level and from that to Assembler and finally to machine code. The question is about "Terminal" which only knows about "key events" (key strokes) and mostly not "mouse events". Are you talking about the "Terminal" app as delivered by macOS? – reichhart Apr 26 '19 at 11:13
  • Thank you for the clarification! Yes I am speaking of the terminal app which is a preinstalled app on mac os. – user10949666 Apr 26 '19 at 11:18
  • I don't know of any way to directly use mouse events in Terminal.app. If you search for "mouse events in terminal" you will find some solutions for xterms. One very stupid workaround would be to use URLs with a webservice as a wrapper. You can e.g. do `echo 'http://www.example.com/yes'` and then on the URL printed you can right-click and then select "Open URL". But that's nasty. – reichhart Apr 26 '19 at 11:22
  • This could help you: https://stackoverflow.com/questions/4230867/how-do-i-simulate-a-mouse-click-through-the-mac-terminal -- this is the other way round but it shows the integration of AppleScript as possible solution. – reichhart Apr 26 '19 at 11:24
  • Thank you so much for your help! – user10949666 Apr 26 '19 at 11:35
  • Search for "Mouse Reporting". This is a feature since 10.11 in Terminal.app. This could help you. Search for "esc sequences" to enable it (keystroke is Cmd-R). – reichhart Apr 26 '19 at 11:39
  • Thank you! I found some things regarding mouse reporting but no documentation or something like that. Would you know where I could find that? – user10949666 Apr 26 '19 at 11:53

1 Answers1

1

Partial answer

To get mouse clicks in terminal you must have "Mouse Reporting" enabled in Terminal.app by Cmd-R. (If you don't want to rely on that you can launch an xterm from within Terminal.app.)

Then you can use this sequence to get mouse clicks transformed to key strokes:

echo -e "\033[?1000h"

Just issue it and then click in Terminal window. To disable it again use this:

echo -e "\033[?1000l"

This CSI sequence has this format: ESC [ ? number l/h

More details can be found here: XTerm Control Sequences

Be aware that Terminal.app won't support every xterm feature.

Quick'n'dirty C code for enabling:

#include <unistd.h>
#define MR "\033[?1000h"
int main() { write(1,MR,sizeof(MR)-1);return 0;}

The "mouse keystrokes" can be read as other key strokes from STDIN.

Alternate solution approach

In addition to C you can use AppleScript.

reichhart
  • 813
  • 7
  • 13
  • That is awesome! Almost everything I need! Would you know about how to pass the key strokes on to a c program? Oh and does this work in c or only in bash? – user10949666 Apr 26 '19 at 12:13
  • This works in Bash, in C and actually in every language which can print the control sequence. Thanks for accepting it as answer. BTW - if you are able to do this then please also upvote it. – reichhart Apr 26 '19 at 16:17