Shop OBEX P1 Docs P2 Docs Learn Events
Keyboard interfacing question — Parallax Forums

Keyboard interfacing question

Devin FrioudDevin Frioud Posts: 17
edited 2013-07-20 14:35 in Propeller 1
Hello,

I was wondering about keyboard input for the propeller;
does the keyboard object return strings or individual characters?

Thanks,
Devin

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-19 18:23
    I was just testing out the keyboard demo myself. The object returns characters.

    The original demo displayed the hex code for the keys pressed.

    My modified version (below) outputs both the character and the hex code.
    CON
    
            _clkmode        = xtal1 + pll16x
            _xinfreq        = 5_000_000
    
    
    
    
    OBJ
    
    
            term    : "tv_terminal"
            kb      : "keyboard"
     
    
    
    PUB start | i
    
    
      'start the tv terminal
      term.start(12)
      term.str(string("Keyboard Demo...",13))
    
    
      'start the keyboard
      kb.start(26, 27)
    
    
      'echo keystrokes in hex
      repeat
        result := kb.getkey
        term.out(result)
        term.out(" ")
        term.out("<")
        term.out("$")
        term.hex(result,3)
        term.out(">")
        term.out(" ")
    
    
    

    If you typed "Hello", the above code would display the following on the TV.
    H <$048> e <$065> l <$06C> l <$06C> o <$06F>
    

    If you only wanted the characters you'd change the repeated section to this:
      repeat 
        term.out(kb.getkey)
    
    
    

    I wrote this demo a couple of hours ago.
  • Devin FrioudDevin Frioud Posts: 17
    edited 2013-07-20 08:18
    Thanks Duane!

    I'll try the code!
    Do you know of any objects that return keyboard input as strings?

    Thanks,
    Devin
  • Mike GreenMike Green Posts: 23,101
    edited 2013-07-20 08:48
    Not really. Frequently, when you have a subroutine that returns a string as the result of a keyboard line or field, you want some simple editing to be performed. For example, you frequently want the backspace key to be interpreted as its usual function. Usually the carriage return character ($0D) performs the end of string function. Because of this editing, you may need to integrate the keyboard input routine with the display (if any) so input characters will be echoed properly. Here's an example taken from FemtoBasic in the ObEx:
    PRI getline | i, c
       i := 0
       repeat
          c := key.getkey
          if c == bspKey
             if i > 0
                dsp.str(string(backspace," ",backspace))
                i--
          elseif c == fReturn
             dsp.out(c)
             tline[i] := 0
             tp := @tline
             return
          elseif i < linelen-1
             dsp.out(c)
             tline[i++] := c
    
    This returns with the input string placed in the buffer "tline" and "tp" is set to the address of the first character. bspKey and backspace are different constants because bspKey is the backspace code returned by the keyboard driver while backspace is the character used by the display object for doing the backspace function (moving the cursor to the left).
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-20 09:33
    Do you know of any objects that return keyboard input as strings?

    Any such object would return a pointer to the string or place the input in a predetermined buffer like Mike's code does. You don't really need an object to do this since now you have Mike's method.

    Just add:
    CON
    MAXSTR_LENGTH = 49
    
    VAR
      long tp
      byte tline[MAXSTR_LENGTH]
    

    To your constant and variable section and use Mike's method to get a string from the keyboard. You can, of course, have "MAXSTR_LENGTH" be any size you want (within reason).
  • Cluso99Cluso99 Posts: 18,069
    edited 2013-07-20 14:19
    On Mike's code, don't forget to limit "i" between the limits of the buffer length, particularly i<0 for backspacing.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-07-20 14:35
    Cluso99 wrote: »
    On Mike's code, don't forget to limit "i" between the limits of the buffer length, particularly i<0 for backspacing.

    It looks like Mike's code checks for zero length prior to backspacing. It looks like he uses "linelen" as the limit to the size of sting. I used "MAXSTR_LENGTH" for this limit since PST uses this. It would have been better if I had used Mike's 'linelen" instead (assuming I'm reading Mike's code correctly).
Sign In or Register to comment.