Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcuts for generate/skip/interrupt #14269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/ui_toprow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def create_inline_toprow_image(self):
def create_prompts(self):
with gr.Column(elem_id=f"{self.id_part}_prompt_container", elem_classes=["prompt-container-compact"] if self.is_compact else [], scale=6):
with gr.Row(elem_id=f"{self.id_part}_prompt_row", elem_classes=["prompt-row"]):
self.prompt = gr.Textbox(label="Prompt", elem_id=f"{self.id_part}_prompt", show_label=False, lines=3, placeholder="Prompt (press Ctrl+Enter or Alt+Enter to generate)", elem_classes=["prompt"])
self.prompt = gr.Textbox(label="Prompt", elem_id=f"{self.id_part}_prompt", show_label=False, lines=3, placeholder="Prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])
self.prompt_img = gr.File(label="", elem_id=f"{self.id_part}_prompt_image", file_count="single", type="binary", visible=False)

with gr.Row(elem_id=f"{self.id_part}_neg_prompt_row", elem_classes=["prompt-row"]):
self.negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"{self.id_part}_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt (press Ctrl+Enter or Alt+Enter to generate)", elem_classes=["prompt"])
self.negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"{self.id_part}_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])

self.prompt_img.change(
fn=modules.images.image_data,
Expand Down
29 changes: 25 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,22 @@ document.addEventListener("DOMContentLoaded", function() {
});

/**
* Add a ctrl+enter as a shortcut to start a generation
* Add keyboard shortcuts:
* Ctrl+Enter to start/restart a generation
* Alt/Option+Enter to skip a generation
* Esc to interrupt a generation
*/
document.addEventListener('keydown', function(e) {
const isEnter = e.key === 'Enter' || e.keyCode === 13;
const isModifierKey = e.metaKey || e.ctrlKey || e.altKey;
const isCtrlKey = e.metaKey || e.ctrlKey;
const isAltKey = e.altKey;
const isEsc = e.key === 'Escape';

const interruptButton = get_uiCurrentTabContent().querySelector('button[id$=_interrupt]');
const generateButton = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
const interruptButton = get_uiCurrentTabContent().querySelector('button[id$=_interrupt]');
const skipButton = get_uiCurrentTabContent().querySelector('button[id$=_skip]');

if (isEnter && isModifierKey) {
if (isCtrlKey && isEnter) {
if (interruptButton.style.display === 'block') {
interruptButton.click();
const callback = (mutationList) => {
Expand All @@ -150,6 +156,21 @@ document.addEventListener('keydown', function(e) {
}
e.preventDefault();
}

if (isAltKey && isEnter) {
skipButton.click();
e.preventDefault();
}

if (isEsc) {
const globalPopup = document.querySelector('.global-popup');
const lightboxModal = document.querySelector('#lightboxModal');
if (!globalPopup || globalPopup.style.display === 'none') {
if (document.activeElement === lightboxModal) return;
interruptButton.click();
e.preventDefault();
}
}
});

/**
Expand Down