Add a :fake-key command.

Closes #556.
See #551.
This commit is contained in:
Florian Bruhin 2015-11-20 07:05:16 +01:00
parent 1ec03462c8
commit af875f4b8f
5 changed files with 110 additions and 0 deletions

View File

@ -51,6 +51,8 @@ Added
- `:set-cmd-text` has a new `--append` argument to append to the current
statusbar text.
- qutebrowser now uses `~/.netrc` if available to authenticate via HTTP.
- New `:fake-key` command to send a fake keypress to a website or to
qutebrowser.
Changed
~~~~~~~

View File

@ -19,6 +19,7 @@
|<<download-open,download-open>>|Open the last/[count]th download.
|<<download-remove,download-remove>>|Remove the last/[count]th download from the list.
|<<download-retry,download-retry>>|Retry the first failed/[count]th download.
|<<fake-key,fake-key>>|Send a fake keypress or key string to the website or qutebrowser.
|<<forward,forward>>|Go forward in the history of the current tab.
|<<fullscreen,fullscreen>>|Toggle fullscreen mode.
|<<help,help>>|Show help about a command or setting.
@ -197,6 +198,20 @@ Retry the first failed/[count]th download.
==== count
The index of the download to cancel.
[[fake-key]]
=== fake-key
Syntax: +:fake-key [*--global*] 'keystring'+
Send a fake keypress or key string to the website or qutebrowser.
:fake-key xy - sends the keychain 'xy' :fake-key <Ctrl-x> - sends Ctrl-x :fake-key <Escape> - sends the escape key
==== positional arguments
* +'keystring'+: The keystring to send.
==== optional arguments
* +*-g*+, +*--global*+: If given, the keys are sent to the qutebrowser UI.
[[forward]]
=== forward
Syntax: +:forward [*--tab*] [*--bg*] [*--window*]+

View File

@ -1684,3 +1684,40 @@ class CommandDispatcher:
message.info(self._win_id, out[:5000] + ' [...trimmed...]')
else:
message.info(self._win_id, out)
@cmdutils.register(instance='command-dispatcher', scope='window')
def fake_key(self, keystring, global_=False):
"""Send a fake keypress or key string to the website or qutebrowser.
:fake-key xy - sends the keychain 'xy'
:fake-key <Ctrl-x> - sends Ctrl-x
:fake-key <Escape> - sends the escape key
Args:
keystring: The keystring to send.
global_: If given, the keys are sent to the qutebrowser UI.
"""
try:
keyinfos = utils.parse_keystring(keystring)
except utils.KeyParseError as e:
raise cmdexc.CommandError(str(e))
for keyinfo in keyinfos:
press_event = QKeyEvent(QEvent.KeyPress, keyinfo.key,
keyinfo.modifiers, keyinfo.text)
release_event = QKeyEvent(QEvent.KeyRelease, keyinfo.key,
keyinfo.modifiers, keyinfo.text)
if global_:
receiver = QApplication.focusWindow()
if receiver is None:
raise cmdexc.CommandError("No focused window!")
else:
try:
receiver = objreg.get('webview', scope='tab',
tab='current')
except KeyError:
raise cmdexc.CommandError("No focused webview!")
QApplication.postEvent(receiver, press_event)
QApplication.postEvent(receiver, release_event)

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fake keypresses</title>
<script type="text/javascript">
window.onload = function () {
keyPressHandler = function (e) {
console.log("key press: " + e.keyCode);
}
keyReleaseHandler = function (e) {
console.log("key release: " + e.keyCode);
}
window.addEventListener('keydown', keyPressHandler, false);
window.addEventListener('keyup', keyReleaseHandler, false);
}
</script>
</head>
<body>
This page logs keypresses via console.log.
</body>
</html>

View File

@ -113,3 +113,37 @@ Feature: Various utility commands.
And I run :inspector
And I run :inspector
Then no crash should happen
# :fake-key
Scenario: :fake-key with an unparsable key
When I run :fake-key <blub>
Then the error "Could not parse 'blub': Got unknown key." should be shown.
Scenario: :fake-key sending key to the website
When I set general -> log-javascript-console to true
And I open data/misc/fakekey.html
And I run :fake-key x
Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 88" should be logged
Scenario: :fake-key sending special key to the website
When I set general -> log-javascript-console to true
And I open data/misc/fakekey.html
And I run :fake-key <Escape>
Then the javascript message "key press: 27" should be logged
And the javascript message "key release: 27" should be logged
Scenario: :fake-key sending keychain to the website
When I set general -> log-javascript-console to true
And I open data/misc/fakekey.html
And I run :fake-key xy
Then the javascript message "key press: 88" should be logged
And the javascript message "key release: 88" should be logged
And the javascript message "key press: 89" should be logged
And the javascript message "key release: 89" should be logged
Scenario: :fake-key sending keypress to qutebrowser
When I run :fake-key -g x
And I wait for "got keypress in mode KeyMode.normal - delegating to <qutebrowser.keyinput.modeparsers.NormalKeyParser>" in the log
Then no crash should happen