From 57271df94c1a1862a019b7dc939b801a723f2ec3 Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Mon, 13 Jun 2016 11:08:44 -0700 Subject: [PATCH] js suggest: better keyup/keydown handling some actions, like ctrl-click, trigger the keyup event even though it has no bearing on autocomplete. This leads to extra impression-results tracking events being fired. The existing keypress event, per spec, handles input that normally produces a character value. The keyup / keydown events should only be handling special non-character producing keys that directly effect autocomplete. As such put together a list of relevant keys and only fire on those. Change-Id: I8d2a4fade84a5b94a81fa7ee0192912d2407ba92 --- resources/src/jquery/jquery.suggestions.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/resources/src/jquery/jquery.suggestions.js b/resources/src/jquery/jquery.suggestions.js index 38e5a1f6b4..212c8f4a72 100644 --- a/resources/src/jquery/jquery.suggestions.js +++ b/resources/src/jquery/jquery.suggestions.js @@ -745,9 +745,23 @@ $.suggestions.keypress( e, context, context.data.keypressed ); } ) .keyup( function ( e ) { - // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a - // keypress in between, solve it - if ( context.data.keypressedCount === 0 ) { + // The keypress event is fired when a key is pressed down and that key normally + // produces a character value. We also want to handle some keys that don't + // produce a character value so we also attach to the keydown/keyup events. + // List of codes sourced from + // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode + var allowed = [ + 40, // up arrow + 38, // down arrow + 27, // escape + 13, // enter + 46, // delete + 8 // backspace + ]; + if ( context.data.keypressedCount === 0 + && e.which === context.data.keypressed + && $.inArray( e.which, allowed ) !== -1 + ) { $.suggestions.keypress( e, context, context.data.keypressed ); } } ) -- 2.20.1