Inkey

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Inkey

Post by cogier »

How do I capture a key press in a CLI program?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Inkey

Post by stevedee »

cogier wrote: Friday 12th May 2017 7:13am How do I capture a key...
This may depend upon the type of cli program. If you use gb.ncurses to make a simple window (like alsamixer) you can respond to events and test the result.

Public Sub qWindow_Read()
Dim iKey As Integer = Window.Read()

If iKey = Key["r"] Then...


Take a look at my recent blog post: http://captainbodgit.blogspot.co.uk/201 ... -text.html
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Inkey

Post by vuott »

Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Inkey

Post by cogier »

Thanks guys but I have not explained my self well. Here is some code in a CLI only program and I would like to press 'Esc', or some other key, to stop the program running.
hTimer As Timer

Public Sub Main()

hTimer = New Timer As "IntTimer"

With hTimer
  .Delay = 500
  .Start
End With

End

Public Sub IntTimer_Timer()

Print Rand(0, 100)

End
I have been looking at http://rosettacode.org/wiki/Rosetta_Code and adding code to the site and this is the 'Task' that I am trying to complete. http://www.rosettacode.org/wiki/Handle_a_signal. The site encouraged me to write the 15 Puzzle Game that is here http://rosettacode.org/wiki/15_Puzzle_Game#Gambas and on the Gambas Farm.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Inkey

Post by stevedee »

cogier wrote: Saturday 13th May 2017 10:34am ...I would like to press 'Esc', or some other key, to stop the program running....
I think you need to use the gb.signal component: http://gambaswiki.org/wiki/comp/gb.signal

I thought I had some example code, but cant find it at the moment. Will try to take a look later.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Inkey

Post by cogier »

Thanks stevedee. Your suggestion of using 'gb.signal' solved my issue. I do feel there must/should be an easier way to capture a keypress.

The code is
hTimer As Timer
fTime As Float

Public Sub Application_Signal(x As Integer)

Print "Program stopped after " & fTime & " seconds"
Quit

End
 
Public Sub Main()
 
hTimer = New Timer As "IntTimer"

Print "Press [Ctrl] + " & Chr(92) & " to stop"

Signal[Signal.SIGQUIT].Catch
 
With hTimer
  .Delay = 500
  .Start
End With

End
 
Public Sub IntTimer_Timer()

Print Rand(0, 100)
fTime += 0.5

End
I have put it on the Rosetta site here http://www.rosettacode.org/wiki/Handle_a_signal#Gambas
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Inkey

Post by stevedee »

cogier wrote: Saturday 13th May 2017 2:14pm ...I do feel there must/should be an easier way to capture a keypress...
Well, the task was about responding to signals, not just reading a key combination (I think some of the programs listed could only check the keys, not respond to signals).

In your example I'd get rid of the "With" & "End With" lines, as it saves 2 lines of code. It probably only makes sense to use this combo if you have 3 or more properties or methods to set/execute.

All-in-all I think your Gambas example stands up very well against the other listed languages on the http://www.rosettacode.org/wiki/Handle_a_signal page.

Well done!

You could also remove some of the blank lines to make it look more compact.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Inkey

Post by cogier »

Interesting point the "x As Integer" issue. It seems it is not needed but Gambas will come up with an error.

If you do include it the IDE tells you that "x is an unused argument". :?

While creating this post I discovered that if the program is run the error occurs but if you press [F5] again there is no complaint!! :o

I'll put a bug report on the 'other' forum.

You do have a point regarding the 'With hTimer..'. I set it up this way so that I could add other properties if needed but they weren't!
SignalError.png
SignalError.png (27.71 KiB) Viewed 12858 times
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Inkey

Post by stevedee »

cogier wrote: Saturday 13th May 2017 3:26pm Interesting point the "x As Integer" issue....
I didn't check whether it would work without it properly...then when it didn't, I tried to correct my post hoping you hadn't already read it. Sorry!
cogier wrote: Saturday 13th May 2017 3:26pm You do have a point regarding the 'With hTimer..'. I set it up this way so that I could add other properties if needed...
...I do exactly the same thing.

Just been looking at all the tasks on that website. Makes you think us Gambasers should pull our fingers out and get coding, and start adding more examples to the list!
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Inkey

Post by cogier »

I didn't check whether it would work without it properly...then when it didn't, I tried to correct my post hoping you hadn't already read it. Sorry!
You had me worried I was thinking 'I am sure he mentioned this'!
Makes you think us Gambasers should pull our fingers out and get coding, and start adding more examples to the list!
I have been working at it. Have a look here http://rosettacode.org/wiki/Category:Gambas. Quite a bit of this is my code. Unfortunately there is someone who has put up Gambas code that wont run and with incorrect comments. Have a look here http://rosettacode.org/wiki/Arrays#Gambas.

Let us know if you put any code up.
Post Reply