在Python中获取辅助输入以控制历史记录

时间:2015-07-18 12:36:30

标签: python python-2.7

我想要建立一个"输入历史记录" Python 2.x中的脚本,但是在使用raw_input()时写入辅助输入并写入用户输入的内容时遇到了一些麻烦。

因为我发现它有点难以解释,所以我在JavaSript + HTML中提供了一个例子来尝试清理它。

JavaScript + HTML示例

(以摘录形式)



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();
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

我不确定是否可以写入用户输入的内容,以便他们可以编辑它等,而无需编写C / ++代码。如果它确实需要C / ++,或者它需要任何奇怪的(但最好是小的)模块,我对它很好。 另外,我在Linux环境中编写了这个程序,所以我对Windows / Mac-only的答案不感兴趣。

1 个答案:

答案 0 :(得分:1)

您所描述的是readline功能。有一些如何使用它的样本here。一旦你了解了这个功能,你可能会发现这已经在SO here上得到了解答。