Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of right now the short answer to your question is there is no way to do this natively in Mousetrap. That being said there are a few ways you could approach this.</p> <ol> <li><p>I am not familiar with KnockoutJS, but browsers are built to let the <code>enter</code> key trigger a form submit so the most elegant solution would probably be to make your inputs part of a form in which case you don't need mousetrap at all to trigger the form submit. You could call <code>e.preventDefault()</code> on form submit to make sure it doesn't actually post back to the server. </p></li> <li><p>If you add class <code>mousetrap</code> to a text input then it will allow it to receive keyboard shortcuts. You could then check <code>e.target</code> in your callback to see if it came from that textarea. This would look something like this:</p> <p><strong>html</strong></p> <pre><code>&lt;input type="text" name="email" class="mousetrap"&gt; </code></pre> <p><strong>js</strong></p> <pre><code>var input = $('input[name=email]'); Mousetrap.bind('enter', function(e) { if (e.target === input[0]) { console.log('triggered from input'); } }); </code></pre></li> <li><p>You could overwrite the <code>Mousetrap.stopCallback</code> function to achieve this. This is not the prettiest solution, but should work.</p> <p><strong>html</strong></p> <pre><code>&lt;input type="text" name="email"&gt; </code></pre> <p><strong>js</strong></p> <pre><code>var input = $('input[name=email]'); var _oldStopCallback = Mousetrap.stopCallback; Mousetrap.stopCallback = function(e, element, combo) { if (combo === 'enter') { return element !== input[0]; } return _oldStopCallback(e, element, combo); } Mousetrap.bind('enter', function(e) { console.log('triggered from input'); }); </code></pre></li> </ol> <p>I am looking into different ways I can support different scopes/contexts in Mousetrap so hopefully there will be an easier way in the future.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload