Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 07, 2014 Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Just out of interest, is there a way of capturing keystrokes in D? E.g. if you have a CLI application that wants to capture Ctrl+... and the like, and possibly control the cursor. |
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 7 February 2014 at 12:17:42 UTC, Chris wrote: > Just out of interest, is there a way of capturing keystrokes in D? E.g. if you have a CLI application that wants to capture Ctrl+... and the like, and possibly control the cursor. You can achieve this by interfacing operating system functions. If your target is windows you can take a look at my ScrollLocker project (https://github.com/PaulFreund/ScrollLocker). It is written in D and uses SetWindowsHookEx and WH_KEYBOARD_LL (low level keyboard hook) to capture key strokes (see helper/hooks.d). There should be similiar facilitys in other OS as well. It should also be possible to get certain combinations in the cmd/shell but I don't know how. |
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Freund | On Friday, 7 February 2014 at 12:32:25 UTC, Paul Freund wrote:
> On Friday, 7 February 2014 at 12:17:42 UTC, Chris wrote:
>> Just out of interest, is there a way of capturing keystrokes in D? E.g. if you have a CLI application that wants to capture Ctrl+... and the like, and possibly control the cursor.
>
> You can achieve this by interfacing operating system functions. If your target is windows you can take a look at my ScrollLocker project (https://github.com/PaulFreund/ScrollLocker). It is written in D and uses SetWindowsHookEx and WH_KEYBOARD_LL (low level keyboard hook) to capture key strokes (see helper/hooks.d). There should be similiar facilitys in other OS as well.
>
> It should also be possible to get certain combinations in the cmd/shell but I don't know how.
Thanks for the link. It's a good starting point. I'm on Linux, so I'll have a look at the corresponding functions.
|
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 7 February 2014 at 12:17:42 UTC, Chris wrote: > Just out of interest, is there a way of capturing keystrokes in D? This isn't so much a D question as a C one - the operating system's C api will give a way, then you use those same functions in D. If you want input from your own terminal, my terminal.d might help. See the demo main here: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/terminal.d#L2007 It captures as much info as possible in real time, optionally including window resize events, mouse motion and clicks, and key presses. (Also key releases on Windows, but such information is /not/ available on Linux.) Ctrl+x keys are sent as as low ascii values. Ctrl+a is cast(char) 1. Ctrl+b is cast(char) 2. Ctrl+c is sent as cast(char) 3 (though note control+c is captured by the OS and sent as an interrupt signal instead - my lib can catch that too). and so on through Ctrl+z which is 26. You can get more information by doing a GUI program but then you'll have to do text output as gui too, so not as easy as cli. Of course, you could write a terminal emulator and have the best of both worlds... i've done it :) https://github.com/adamdruppe/terminal-emulator But I think terminal.d is what you want. Now, if you want to capture input from ANY window, not just your own, on Windows you can use the API to capture the keyboard, on X you can listen to events on the root window, and on Linux you can also read /dev/input to get raw keyboard events (if you like, I have a small lib that can help with this too). Note that reading /dev/input needs your program to be root. |
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 7 February 2014 at 15:40:06 UTC, Adam D. Ruppe wrote: > On Friday, 7 February 2014 at 12:17:42 UTC, Chris wrote: >> Just out of interest, is there a way of capturing keystrokes in D? > > This isn't so much a D question as a C one - the operating system's C api will give a way, then you use those same functions in D. > > If you want input from your own terminal, my terminal.d might help. See the demo main here: > > https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/terminal.d#L2007 > > > It captures as much info as possible in real time, optionally including window resize events, mouse motion and clicks, and key presses. (Also key releases on Windows, but such information is /not/ available on Linux.) > > Ctrl+x keys are sent as as low ascii values. Ctrl+a is cast(char) 1. Ctrl+b is cast(char) 2. Ctrl+c is sent as cast(char) 3 (though note control+c is captured by the OS and sent as an interrupt signal instead - my lib can catch that too). and so on through Ctrl+z which is 26. > > > You can get more information by doing a GUI program but then you'll have to do text output as gui too, so not as easy as cli. Of course, you could write a terminal emulator and have the best of both worlds... i've done it :) > > https://github.com/adamdruppe/terminal-emulator > > > But I think terminal.d is what you want. > > > > Now, if you want to capture input from ANY window, not just your own, on Windows you can use the API to capture the keyboard, on X you can listen to events on the root window, and on Linux you can also read /dev/input to get raw keyboard events (if you like, I have a small lib that can help with this too). Note that reading /dev/input needs your program to be root. I just had a look at terminal.d. Great stuff. It works, though it sometimes stops working and gives me this exception (after killing it with Ctrl+C): object.Exception@terminal.d(814): write failed for some reason There is a syntax error on line 54; version = Demo should be version = Demo; I'll have a look at your other stuff too, it looks very interesting. |
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 7 February 2014 at 16:21:20 UTC, Chris wrote:
> On Friday, 7 February 2014 at 15:40:06 UTC, Adam D. Ruppe wrote:
>> On Friday, 7 February 2014 at 12:17:42 UTC, Chris wrote:
>>> Just out of interest, is there a way of capturing keystrokes in D?
>>
>> This isn't so much a D question as a C one - the operating system's C api will give a way, then you use those same functions in D.
>>
>> If you want input from your own terminal, my terminal.d might help. See the demo main here:
>>
>> https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/terminal.d#L2007
>>
>>
>> It captures as much info as possible in real time, optionally including window resize events, mouse motion and clicks, and key presses. (Also key releases on Windows, but such information is /not/ available on Linux.)
>>
>> Ctrl+x keys are sent as as low ascii values. Ctrl+a is cast(char) 1. Ctrl+b is cast(char) 2. Ctrl+c is sent as cast(char) 3 (though note control+c is captured by the OS and sent as an interrupt signal instead - my lib can catch that too). and so on through Ctrl+z which is 26.
>>
>>
>> You can get more information by doing a GUI program but then you'll have to do text output as gui too, so not as easy as cli. Of course, you could write a terminal emulator and have the best of both worlds... i've done it :)
>>
>> https://github.com/adamdruppe/terminal-emulator
>>
>>
>> But I think terminal.d is what you want.
>>
>>
>>
>> Now, if you want to capture input from ANY window, not just your own, on Windows you can use the API to capture the keyboard, on X you can listen to events on the root window, and on Linux you can also read /dev/input to get raw keyboard events (if you like, I have a small lib that can help with this too). Note that reading /dev/input needs your program to be root.
>
> I just had a look at terminal.d. Great stuff. It works, though it sometimes stops working and gives me this exception (after killing it with Ctrl+C):
>
> object.Exception@terminal.d(814): write failed for some reason
>
> There is a syntax error on line 54;
>
> version = Demo
>
> should be
>
> version = Demo;
>
> I'll have a look at your other stuff too, it looks very interesting.
It actually breaks when I press a modifier key like Ctrl.
|
February 07, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 7 February 2014 at 16:58:48 UTC, Chris wrote:
> It actually breaks when I press a modifier key like Ctrl.
Ctrl alone or ctrl+ a key? Also which terminal emulator and any error message?
|
February 09, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 7 February 2014 at 18:03:06 UTC, Adam D. Ruppe wrote:
> On Friday, 7 February 2014 at 16:58:48 UTC, Chris wrote:
>> It actually breaks when I press a modifier key like Ctrl.
>
> Ctrl alone or ctrl+ a key? Also which terminal emulator and any error message?
I used the xfce terminal. If I remember correctly, it would happen after just pressing Ctrl. It doesn't happen always but often enough. I'll check the details when I'm back at the machine I used.
|
February 09, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On 2/7/14, 6:17, Chris wrote: > Just out of interest, is there a way of capturing keystrokes in D? E.g. > if you have a CLI application that wants to capture Ctrl+... and the > like, and possibly control the cursor. Check the Deimos github repo. (github.com/D-Programming-Deimos/ncurses I think) Ncurses bindings live in there. It can do everything you mention and more. If you decide to use it, let me know if you find any bugs. |
February 10, 2014 Re: Capturing keystrokes | ||||
---|---|---|---|---|
| ||||
Posted in reply to 1100110 | On Sunday, 9 February 2014 at 22:01:46 UTC, 1100110 wrote:
> On 2/7/14, 6:17, Chris wrote:
>> Just out of interest, is there a way of capturing keystrokes in D? E.g.
>> if you have a CLI application that wants to capture Ctrl+... and the
>> like, and possibly control the cursor.
>
> Check the Deimos github repo. (github.com/D-Programming-Deimos/ncurses I think)
>
> Ncurses bindings live in there. It can do everything you mention and more. If you decide to use it, let me know if you find any bugs.
Brilliant, thanks. It completely escaped me that ncurses was in the Deimos repo.
|
Copyright © 1999-2021 by the D Language Foundation