Inkey
Re: Inkey
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
Re: Inkey
You might also try some other ways:
- http://www.gambas-it.org/wiki/index.php ... di_comando
- http://www.gambas-it.org/wiki/index.php ... di_comando
- http://www.gambas-it.org/wiki/index.php ... e_termio.h
- http://www.gambas-it.org/wiki/index.php ... _termios.h
- http://www.gambas-it.org/wiki/index.php ... di_comando
- http://www.gambas-it.org/wiki/index.php ... di_comando
- http://www.gambas-it.org/wiki/index.php ... e_termio.h
- http://www.gambas-it.org/wiki/index.php ... _termios.h
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Inkey
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.Re: Inkey
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.
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Inkey
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
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#GambasRe: Inkey
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.
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Inkey
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!!
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!
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!!
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!
Re: Inkey
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!
...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!
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Inkey
You had me worried I was thinking 'I am sure he mentioned this'!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!
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.Makes you think us Gambasers should pull our fingers out and get coding, and start adding more examples to the list!
Let us know if you put any code up.