Testing keyboard events

by litespeedtdf

HTML

<div>Username</div>
<input id="username" class="mousetrap" type = "text" autocomplete="off" type="text" />
<div>Results</div>
<textarea id="result" rows="20" cols="50"></textarea >
 <button id="clear">Clear</button>
  <button id="txt">TextRange</button>
   <button id="onOff">Turn events </button>

JavaScript

/**
 * jquery-textrange
 *
 * A jQuery plugin for getting, setting and replacing the selected text in input fields and textareas.
 * See the [README](https://github.com/dwieeb/jquery-textrange/blob/1.x/README.md) for usage and examples.
 *
 * (c) 2012-2017 Daniel Imhoff <[email protected]> - dwieeb.com
 */

(function(factory) {

	if (typeof define === 'function' && define.amd) {
		define(['jquery'], factory);
	} else if (typeof exports === 'object') {
		factory(require('jquery'));
	} else {
		factory(jQuery);
	}

})(function($) {

	var browserType,

	textrange = {

		/**
		 * $().textrange() or $().textrange('get')
		 *
		 * Retrieves an object containing the start and end location of the text range, the length of the range and the
		 * substring of the range.
		 *
		 * @param (optional) property
		 * @return An object of properties including position, start, end, length, and text or a specific property.
		 */
		get: function(property) {
			return _textrange[browserType].get.apply(this, [property]);
		},

		/**
		 * $().textrange('set')
		 *
		 * Sets the selected text of an object by specifying the start and length of the selection.
		 *
		 * The start and length parameters are identical to PHP's substr() function with the following changes:
		 *  - excluding start will select all the text in the field.
		 *  - passing 0 for length will set the cursor at start. See $().textrange('setcursor')
		 *
		 * @param (optional) start
		 * @param (optional) length
		 *
		 * @see https://secure.php.net/manual/en/function.substr.php
		 */
		set: function(start, length) {
			var s = parseInt(start),
			    l = parseInt(length),
			    e;

			if (typeof start === 'undefined') {
				s = 0;
			} else if (start < 0) {
				s = this[0].value.length + s;
			}

			if (typeof length !== 'undefined') {
				if (length >= 0) {
					e = s + l;
				} else {
					e = this[0].value.length + l;
				}
			}

			_textrange[browserType].set.apply(this, [s, e]);

			return this;
		},

		/**
		 *...