Implement mobile touch keypad in javascript

Martin

I want to implement mobile keypad in java script or jquery. I am implement basic structure for keypad But i am not clear about javascript.

$("#phone").find("button").mouseup(function(event){
  var button_pressed = $(event.currentTarget).data("value")
  $("#result").val(t9($("#result").val(),button_pressed))
})

function t9(text,button_pressed){
  // Write your code here
  return text
}
#phone button {
  height: 50px;
  width: 75px;
}

#phone button span {
  display: block;
  margin-top: 5px;
  font-size: 10px;
}

#result {
  width: 225px;
  height: 25px;
  margin-left:2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table id="phone">
  <tr>
    <td colspan="3">
      <input type="text" id="result" />
    </td>
  </tr>
  <tr>
    <td>
      <button data-value="1" class="key">1
        <span>. , !</span>
      </button>
    </td>
    <td>
      <button data-value="2" class="key">2
        <span>a b c</span>
      </button>
    </td>
    <td>
      <button data-value="3" class="key">3
        <span>d e f</span>
      </button>
    </td>
  </tr>
  <tr>
    <td>
      <button data-value="4" class="key">4
        <span>g h i</span>
      </button>
    </td>
    <td>
      <button data-value="5" class="key">5
        <span>j k l</span>
      </button>
    </td>
    <td>
      <button data-value="6" class="key">6
        <span>m n o</span>
      </button>
    </td>
  </tr>
  <tr>
    <td><button data-value="7" class="key">7
      <span>p q r s</span>
      </button>
    </td>
    <td>
      <button data-value="8" class="key">8
        <span>t u v</span>
      </button>
    </td>
    <td>
      <button data-value="9" class="key">9
        <span>w x y z</span>
      </button>
    </td>
  </tr>
  <tr>
    <td><button data-value="*" class="key">*</button></td>
    <td><button data-value="0" class="key">0</button></td>
    <td><button data-value="#" class="key">#</button></td>
  </tr>
</table>

Mosh Feu
  1. Replace data('value') width attr('data-value').
  2. return button_pressed instead of text in t9 function.
  3. Optional - You can replace $(event.currentTarget) with $(this)

var to = 1000, timeout, counter = 0, lastKey, keyPressTimeout, keyPressTO = 1000;

$("#phone button").bind("mousedown", function() {
  var $this = $(this),
      $result = $('#result'),
      val = $result.val(),
      button_pressed = $this.attr("data-value");

  keyPressTimeout = setTimeout(function() {
    // if the click is long add the value of the button to the textxbox
    val += button_pressed;
    $result.val(val);
    keyPressTimeout = null;
  }, keyPressTO);
  
}).bind("mouseup", function(event) {
  clearTimeout(keyPressTimeout);
  
  if (!keyPressTimeout) {
    return false;
  }
  var $this = $(this),
      $result = $('#result'),
      val = $result.val(),
      button_pressed = $this.attr("data-value");

  // if the user clicks on a new key reset all
  if (lastKey !== button_pressed) {
    reset();  
  }

  // if the user click fast on the same key, remove the last charchter to replace it with the new
  if (counter !== 0 && lastKey === button_pressed) {
    val = val.substring(0, val.length - 1);
  }

  val += t9(button_pressed);
  $result.val(val);

  // restart the timeout
  clearTimeout(timeout);
  counter++;

  // save the last key pressed so we can compare it in the next click
  lastKey = button_pressed;

  // if the user not clicked on anything within the timeout delay (to variable) reset all.
  timeout = setTimeout(reset, to);
});

function t9(button_pressed) {
  return keys[button_pressed][counter % keys[button_pressed].length];
}

function reset() {
  counter = 0;
  lastKey = null;
}

var keys = {
  '1': ['.', ',', '!'],
  '2': ['a', 'b', 'c']
};
#phone button {
  height: 50px;
  width: 75px;
}
#phone button span  {
  display: block;
  margin-top: 5px;
  font-size: 10px;
}
#result{
  width: 225px;
  height: 25px;
  margin-left:2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<table id="phone">
  <tr>
    <td colspan="3">
      <input type="text" id="result" />
    </td>
  </tr>
  <tr>
    <td>
      <button data-value="1" class="key">1
        <span>. , !</span>
      </button>
    </td>
    <td>
      <button data-value="2" class="key">2
        <span>a b c</span>
      </button>
    </td>
    <td>
      <button data-value="3" class="key">3
        <span>d e f</span>
      </button>
    </td>
  </tr>
  <tr>
    <td>
      <button data-value="4" class="key">4
        <span>g h i</span>
      </button>
    </td>
    <td>
      <button data-value="5" class="key">5
        <span>j k l</span>
      </button>
    </td>
    <td>
      <button data-value="6" class="key">6
        <span>m n o</span>
      </button>
    </td>
  </tr>
  <tr>
    <td><button data-value="7" class="key">7
      <span>p q r s</span>
      </button>
    </td>
    <td>
      <button data-value="8" class="key">8
        <span>t u v</span>
      </button>
    </td>
    <td>
      <button data-value="9" class="key">9
        <span>w x y z</span>
      </button>
    </td>
  </tr>
  <tr>
    <td><button data-value="*" class="key">*</button></td>
    <td><button data-value="0" class="key">0</button></td>
    <td><button data-value="#" class="key">#</button></td>
  </tr>
</table>

http://output.jsbin.com/seqexi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creation of mobile keypad for touchscreen using jquery

From Dev

Mobile touch behaviour

From Dev

onClick not working on mobile (touch)

From Dev

Submenu not showing in mobile touch

From Dev

Touch input on mobile device

From Dev

Creating JavaScript Phone Keypad with Alphabet for SMS

From Dev

Creating JavaScript Phone Keypad with Alphabet for SMS

From Dev

Solve numeric keypad puzzle using javascript

From Dev

Prevent <textarea> from losing focus to keep mobile keypad

From Dev

How to Type Multilingual Characters in Windows Mobile Device by Device Keypad

From Dev

Change the size of an element by mouse(or touch in mobile) using javascript (or css ,that's better if possible), not considering the jquery ui

From Dev

Sencha Touch over jquery Mobile

From Dev

Touch events for google maps on mobile

From Dev

Deprecated touch operations on mobile apps

From Dev

Touch events for google maps on mobile

From Dev

Implement routing in Sencha Touch 2.4.0

From Dev

How to Implement touch on a hybrid app

From Dev

How to Have Drupal WebForm phone number input show a keypad on a mobile device

From Dev

How to implement jQuery Mobile Animated Ajax page navigation (transitions) in plain javascript

From Java

Changing :hover to touch/click for mobile devices

From Dev

Mobile slider only responding every other touch

From Dev

Google maps iframe disable scrolling touch on mobile

From Dev

Disable touch panning for body of site on mobile devices

From Dev

Disable fullpage.js on mobile (touch) devices

From Dev

Canvas get touch position on mobile web

From Dev

jQuery drag and drop is not working with mobile touch

From Dev

Controlling a player using touch input on the mobile device

From Dev

How to use Whatsapp mobile application on Ubuntu Touch?

From Dev

Click vs touch on mobile devices in JS

Related Related

  1. 1

    Creation of mobile keypad for touchscreen using jquery

  2. 2

    Mobile touch behaviour

  3. 3

    onClick not working on mobile (touch)

  4. 4

    Submenu not showing in mobile touch

  5. 5

    Touch input on mobile device

  6. 6

    Creating JavaScript Phone Keypad with Alphabet for SMS

  7. 7

    Creating JavaScript Phone Keypad with Alphabet for SMS

  8. 8

    Solve numeric keypad puzzle using javascript

  9. 9

    Prevent <textarea> from losing focus to keep mobile keypad

  10. 10

    How to Type Multilingual Characters in Windows Mobile Device by Device Keypad

  11. 11

    Change the size of an element by mouse(or touch in mobile) using javascript (or css ,that's better if possible), not considering the jquery ui

  12. 12

    Sencha Touch over jquery Mobile

  13. 13

    Touch events for google maps on mobile

  14. 14

    Deprecated touch operations on mobile apps

  15. 15

    Touch events for google maps on mobile

  16. 16

    Implement routing in Sencha Touch 2.4.0

  17. 17

    How to Implement touch on a hybrid app

  18. 18

    How to Have Drupal WebForm phone number input show a keypad on a mobile device

  19. 19

    How to implement jQuery Mobile Animated Ajax page navigation (transitions) in plain javascript

  20. 20

    Changing :hover to touch/click for mobile devices

  21. 21

    Mobile slider only responding every other touch

  22. 22

    Google maps iframe disable scrolling touch on mobile

  23. 23

    Disable touch panning for body of site on mobile devices

  24. 24

    Disable fullpage.js on mobile (touch) devices

  25. 25

    Canvas get touch position on mobile web

  26. 26

    jQuery drag and drop is not working with mobile touch

  27. 27

    Controlling a player using touch input on the mobile device

  28. 28

    How to use Whatsapp mobile application on Ubuntu Touch?

  29. 29

    Click vs touch on mobile devices in JS

HotTag

Archive