Shop OBEX P1 Docs P2 Docs Learn Events
Help with using Keyboard Object — Parallax Forums

Help with using Keyboard Object

SELSEL Posts: 80
edited 2011-07-12 08:28 in Propeller 1
Guys,

keyboard's getkey method returns one char at a time.
I need to type in 123 capturing the chars and convert them to a numeric 123.

In the other high level languages I've used you would type 123 then use a string to numeric method to get the result you want. I am stumped as to how to proceed. I've tried many hours to get the results I want.

Someone must have solved this problem using the Propeller?

I would appreciate any help you can give to me in this matter.

Thanks

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-07-11 17:23
    Could you use the Numbers object in the OBEX?

    http://obex.parallax.com/objects/23/
  • Mike GreenMike Green Posts: 23,101
    edited 2011-07-11 17:25
    Look in the Propeller Object Exchange. There are several objects that have routines that do what you want. They're not specifically written for the PS/2 keyboard, but you can easily change the call that they use to get a character to use getkey instead. Look at FullDuplexSerialPlus and BS2_Functions for a start. I'm sure there are others as well.
  • SELSEL Posts: 80
    edited 2011-07-11 18:24
    ElectricAye and Mike Green,

    I do thank you for taking the time to respond!

    Here is my problem: I have tried to use Numbers. In other programs I have used, Nums.FromStr and Nums.ToStr ect., but this does not help me. and Mike If I were as great at programing the Propeller as you are we would not be having this discussion! I took a look at FullDuplexSerialPlus. If I could adapt any of that I would most likely be in your company! Well, from what I wrote above I am not in your company! So, I need some basic help if possible?

    It would have helped if I had mentioned that I am using TV_Text and have created a menu system that gets its input from a keyboard. All is well until I need to type a number to pass to a variable. If all I needed to pass was a char I know how to handle that!

    I hope that you fellows will give this another look.

    Thanks
  • AribaAriba Posts: 2,690
    edited 2011-07-11 19:38
    This code should do what you want:
    num := 0
      repeat
        char := kb.getkey
        if char => "0" and char =< "9"
          num := num * 10 + char - "0"
          'tv.out(char)
      until char == 13
    
    It expects a <return> to terminate the number input.
    Every number is echoed with TV_Text if uncomment the tv.out(char).

    Andy
  • SELSEL Posts: 80
    edited 2011-07-11 20:09
    Andy,

    Thank you very much!! This is elegant in its simplicity.
    I will try this tomorrow and post the results.

    I would be interested in any other solutions, just for the sake of having others for learning purposes.

    Thanks to all
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-07-11 23:23
    Kye wrote a whole library of string drivers. I think the one that might be useful here is this one
    PUB decimalToInteger(characters) | sign '' 10 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts a decimal string into an integer number. Expects a string with only "+-0123456789" characters.
    '' //
    '' // If the string has a "-" sign as its leading character the converted integer returned will be negated.
    '' //
    '' // If the string has a "+" sign as its leading character the converted integer returned will not be negated.
    '' //
    '' // Returns the converted integer. By default the number returned is positive and the "+" sign is unnecessary.
    '' //
    '' // Characters - A pointer to the decimal string to convert. The number returned will be 2's complement compatible.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      characters := checkSign(ignoreSpace(characters), @sign)
    
      repeat (strsize(characters) <# 10)
        ifnot(checkDigit(characters, "0", "9"))
          quit
    
        result := ((result * 10) + (byte[characters++] & $F))
      result *= sign
    

    If you want the whole object, it is inside the zip file on this thread http://forums.parallax.com/showthread.php?132901-Where-is-KyeDOS-latest-vesion post #6, called ASCIIO_STREngine
  • SELSEL Posts: 80
    edited 2011-07-12 08:28
    Dr_Acula,

    Thank you for the direction. I have downloaded both and will study them.
    I admire Kye's work, but for a "newbie" he's way beyond. I realize that he is not creating objects for "newbies" but as we progress hopefully we will understand. I have kept all his work that I can find in hopes of using his objects in the future.

    I started programming in 1972 and processed from there. I am good with high level programing languages. This is my first with the nuts and bolts approach.

    Once, again thank you to all!

    Ariba (Andy) your algorithm was the perfect fit for me and is now part of my menu application! THANKS!!

    Stan
Sign In or Register to comment.