From addadf8dfe2ee10570fa52d8ff451dcda493508c Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Sun, 7 Sep 2025 12:59:48 +0200 Subject: [PATCH] fix: grab only unmodified esc/leftarrow/rightarrow This avoid hijacking for example Alt-Left, which is typically Back in the browser. lightGallery only uses the plain Esc, LeftArrow, RightArrow keys, and should not grab them with any modifiers. We now also use the KeyboardEvent.key property, instead of the deprecated keyCode. The key property has been available across browsers since 2017. Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key Closes https://github.com/sachinchoolur/lightGallery/issues/1732 --- src/lightgallery.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lightgallery.ts b/src/lightgallery.ts index c36fd5fc..30059104 100644 --- a/src/lightgallery.ts +++ b/src/lightgallery.ts @@ -2132,10 +2132,17 @@ export class LightGallery { keyPress(): void { $LG(window).on(`keydown.lg.global${this.lgId}`, (e) => { + const noModifier = !( + e.altKey || + e.ctrlKey || + e.metaKey || + e.shiftKey + ); if ( this.lgOpened && this.settings.escKey === true && - e.keyCode === 27 + e.key === 'Escape' && + noModifier ) { e.preventDefault(); if ( @@ -2149,12 +2156,12 @@ export class LightGallery { } } if (this.lgOpened && this.galleryItems.length > 1) { - if (e.keyCode === 37) { + if (e.key === 'ArrowLeft' && noModifier) { e.preventDefault(); this.goToPrevSlide(); } - if (e.keyCode === 39) { + if (e.key === 'ArrowRight' && noModifier) { e.preventDefault(); this.goToNextSlide(); }