Keyboard prompt sometimes ignores return key?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
j1sh
Posts: 3
Joined: Mon Dec 12, 2016 6:32 pm
Location: Alabama

Keyboard prompt sometimes ignores return key?

Post by j1sh »

Hello, this is my first post, sorry in advance for any grievances.

I'm playing around with the BareBones tutorial from the wiki to learn, and I've pretty much gotten a keyboard "listener" working, but I'm having trouble with a prompt system I want to implement.

Image

My C/C++ is rusty, but anyways, everything works fine in getScancode(), it's when I pass the char to prompt() that it starts being odd. I am trying to make the loop in prompt() stop when the user hits RETURN. (This also happens if I change the key I want detected to something else) For some reason, the loop doesn't sometimes acknowledge what input is. Did I do something dumb?

Here is the code:

Code: Select all

// I have omitted the terminal.h because it's mostly irrelevant for this, and it works like it should

#if !defined(__cplusplus)
#include <stdbool.h> /* C doesn't have booleans by default. */
#endif
#include <stddef.h>
#include <stdint.h>
#include "terminal.h"

#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
 
#if !defined(__i386__)
#error "This needs to be compiled with a ix86-elf compiler"
#endif

unsigned char keyset[128] = {
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
  '9', '0', '-', '=', '\b', /* Backspace */
  '\t',         /* Tab */
  'q', 'w', 'e', 'r',   /* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
    0,          /* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
 '\'', '`',   0,        /* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',            /* 49 */
  'm', ',', '.', '/',   0,              /* Right shift */
  '*',
    0,  /* Alt */
  ' ',  /* Space bar */
    0,  /* Caps lock */
    0,  /* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,  /* < ... F10 */
    0,  /* 69 - Num lock*/
    0,  /* Scroll Lock */
    0,  /* Home key */
    0,  /* Up Arrow */
    0,  /* Page Up */
  '-',
    0,  /* Left Arrow */
    0,
    0,  /* Right Arrow */
  '+',
    0,  /* 79 - End key*/
    0,  /* Down Arrow */
    0,  /* Page Down */
    0,  /* Insert Key */
    0,  /* Delete Key */
    0,   0,   0,
    0,  /* F11 Key */
    0,  /* F12 Key */
    0,  /* All other keys are undefined */
}; 


#if defined(__cplusplus)
extern "C" { /* Use C linkage for kernel_main. */
#endif

static inline void outb(uint16_t port, uint8_t val) {
    asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
}

static inline uint8_t inb(uint16_t port) {
    uint8_t ret;
    asm volatile ( "inb %1, %0"
                   : "=a"(ret)
                   : "Nd"(port) );
    return ret;
}

// initialize the terminal
Terminal term;

char last = 0;

char getScancode() {
	
    char c=0;
	if(inb(0x60)!=c) {
		c=inb(0x60);
        if (c!= last) {
            last = c;
            if (c>0) {
                if (keyset[c] == '\n') {
                    term.WriteString("(Enter Pressed)");
                } else {
                    term.PutChar(keyset[c]);
                }
                return keyset[c];
            }
		}
	}
}

void prompt() {
	term.PutChar('>');
	
	char input=0;

	while (input != '\n'){

		if (getScancode()) {
            input = getScancode();
		}
	}
    term.PutChar('\n');

}

void kernel_main(void) {
	/* Initialize terminal interface */
	

	term.Initialize();
 
	/* Newline support is left as an exercise. */
	term.WriteString("|Ready!\n");

    while(true)
        prompt();

}

}
Just on the border of your waking mind, There lies another time. Where darkness and light are one. As you tread the halls of sanity, You feel so glad to be, Unable to go beyond.
https://github.com/j0shgit
https://en.lichess.org/@/j0sh666
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Keyboard prompt sometimes ignores return key?

Post by Octocontrabass »

j1sh wrote:

Code: Select all

		if (getScancode()) {
            input = getScancode();
		}
If the first call to getScancode() returns '\n' and the second call to getScancode() returns something else, what happens?

(You have another bug like this elsewhere in your code. Can you spot it?)
User avatar
j1sh
Posts: 3
Joined: Mon Dec 12, 2016 6:32 pm
Location: Alabama

Re: Keyboard prompt sometimes ignores return key?

Post by j1sh »

Oh wow I feel dumb. Thanks very much, it works exactly like it should now.

Now I just gotta find the other bug... :oops:

Edit: Found it! Thanks again.
Just on the border of your waking mind, There lies another time. Where darkness and light are one. As you tread the halls of sanity, You feel so glad to be, Unable to go beyond.
https://github.com/j0shgit
https://en.lichess.org/@/j0sh666
Post Reply