Reputation: 40
I'm wanting to build a "input history" script within Python 2.x, but I'm having a bit of trouble getting a secondary input while using raw_input()
, and writing to what the user inputs.
Because I found it a wee bit difficult to explain, I'm providing an example in JavaSript + HTML to try and clear things up.
(In snippet form)
var input = document.getElementById("input"),
button = document.getElementById("button"),
ihistory = document.getElementById("ihistory"),
history = [],
position = 0,
text = "";
button.onclick = function() {
if (input.value != "") {
history.push(input.value);
input.value = "";
position++;
input.focus();
}
};
input.onkeydown = function(e) {
if (e.keyCode == 38 && position - 1 >= 0) {
e.preventDefault();
position--;
input.value = history[position].toString();
} else if (e.keyCode == 40 && position + 1 <= history.length) {
e.preventDefault();
position++;
input.value = history[position].toString();
} else if (e.keyCode == 13) {
button.click();
}
}
<input type="text" id="input"></input>
<button id="button">Submit</button>
<br />
<br />
<br />
<hr />
<p>To Submit text to the text to the history, press "submit".</p>
<p>To access previous history, press the up arrow key. To access future history, press the down arrow key.</p>
I'm not sure it is possible to write to what the user inputs, so that they can edit it, etc, without writing C/++ code. I'm fine with it if it does require C/++, or if it requires any weird (but preferably small) modules. Also, I'm progamming this in a Linux environment, so I am not interested it Windows/Mac-only answers.
Upvotes: 0
Views: 182
Reputation: 13619
What you're describing is readline functionality. There are some samples of how to use it here. Once you've got your head around the function, you'll probably find that this is already answered on SO here.
Upvotes: 1