Inkey

Post

Posted
Rating:
#1 (In Topic #116)
Avatar
Guru
cogier is in the usergroup ‘Guru’
How do I capture a key press in a CLI program?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

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: Captain Bodgit: Gambas cli programming: ncurses text based user interface
Online now: No Back to the top

Post

Posted
Rating:
#3
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Code (gambas)

  1. hTimer As Timer
  2.  
  3. Public Sub Main()
  4.  
  5. hTimer = New Timer As "IntTimer"
  6.  
  7. With hTimer
  8.   .Delay = 500
  9.   .Start
  10.  
  11.  
  12. Public Sub IntTimer_Timer()
  13.  
  14. Print Rand(0, 100)
  15.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

…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: /comp/gb.signal - Gambas Documentation

I thought I had some example code, but cant find it at the moment. Will try to take a look later.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
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

Code (gambas)

  1. hTimer As Timer
  2. fTime As Float
  3.  
  4. Public Sub Application_Signal(x As Integer)
  5.  
  6. Print "Program stopped after " & fTime & " seconds"
  7.  
  8.  
  9. Public Sub Main()
  10.  
  11. hTimer = New Timer As "IntTimer"
  12.  
  13. Print "Press [Ctrl] + " & Chr(92) & " to stop"
  14.  
  15. Signal[Signal.SIGQUIT].Catch
  16.  
  17. With hTimer
  18.   .Delay = 500
  19.   .Start
  20.  
  21.  
  22. Public Sub IntTimer_Timer()
  23.  
  24. Print Rand(0, 100)
  25. fTime += 0.5
  26.  
  27.  

I have put it on the Rosetta site here http://www.rosettacode…ki/Handle_a_signal#Gambas
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

…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 Handle a signal - Rosetta Code page.

Well done!

You could also remove some of the blank lines to make it look more compact.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
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!
 
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

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 said

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!
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Regular
Cedron is in the usergroup ‘Regular’
Attachment

Hi Cogier,

Are you still looking for an inkey function?

The attached project is incomplete, but has the information you need.  The trick is to set the terminal to non-canonical so it doesn't wait for a newline to send.

Ced

.... and carry a big stick!
Online now: No Back to the top
1 guest and 0 members have just viewed this.