Keyboard Error Sound when catching KeyCode - javascript

I'm programming something withe Javascript which captures keyboard input, but the problem is that every time the user presses a key there an error sound. How can I disable it?

#Polaris878
function KeyPressed(e)
{
if (!e) e = window.event;
if (e.which)
{
keycode = e.which
}
else if (e.keyCode)
{
keycode = e.keyCode
}
switch (keycode)
{
case 49:
key = "1";
break;
case 50:
key = "2";
break;
case 51:
key = "3";
break;
case 52:
key = "4";
break;
case 53:
key = "5";
break;
case 54:
key = "6";
break;
case 55:
key = "7";
break;
case 56:
key = "8";
break;
case 57:
key = "9";
break;
case 48:
key = "0";
break;
default:
key = "";
return false;
break
}
if (keys == "NULL")
{
keys = key
}
else
{
keys = keys + key
} if (keys.length >= 5)
{
document.formular.submit();
}
document.formular.code.value = keys;
}
var keys = "";
document.onkeydown = KeyPressed

Related

Event .key switch for a video game

i want to change the way my Js game works, basically the game does not recognize input whenever the language is not english with no Capital letters ( only when the event.key is equal to wasd ) how can i fix this bug ?
thanks !
window.addEventListener('keydown', (event) => {
if (!player.isDead){
switch (event.key) {
case 'w':
keys.d.pressed = true;
player.lastkey = 'd';
break;
case 'a':
keys.a.pressed = true;
player.lastkey = 'a';
break;
case 'w':
if(player.position.y > 0)
{
player.velocity.y = -10;
}
break;
case ' ':
player.Attacking();
if(player.lastkey === 'd'){player.SwitchSprite('punch')}
else{player.SwitchSprite('fpunch')}
break;
}
}
You should use the event.code or event.keyCode properties to stay language independent;
switch (event.code) // <- not event.key
case "KeyW": // <- not 'w'
...

Submit form via AJAX using plain Javascript without Jquery get checked radio button value

I do have HTML form submitting via AJAX using plain Javascript:
function XMLhttp(){
var formInputs = document.getElementById(formID).querySelectorAll("input, textarea");
var selectFormGebdat = formID;
var toCheckDatesFields = document.getElementsByClassName("checkDate");
for (var i = 0; i < toCheckDatesFields.length; i++) {
if(toCheckDatesFields.item(i).value != '') {
var returnValue = validatedate(toCheckDatesFields.item(i));
if(returnValue == false) {
return false;
}
}
}
var httpRequest = new XMLHttpRequest();
var formData = new FormData();
for( var i=0; i < formInputs.length; i++ ){
formData.append(formInputs[i].name, formInputs[i].value);
}
httpRequest.onreadystatechange = function(){
if ( this.readyState == 4 && this.status == 200 ) {
resultData = JSON.parse(this.responseText);
if(resultData.success == true) {
document.getElementById('resultok').className += ' show';
}
else {
document.getElementById('resulterror').className += ' show';
};
}
};
httpRequest.open(formMethod, formAction);
httpRequest.send(formData);
}
selectButton.onclick = function(){
XMLhttp();
}
selectForm.onsubmit = function(){
return false;
}
My problem is, that I have radio buttons and select-box. But the selected values are not submitted. F.ex.
<input type="radio" name="typ" value="anfrage"><input type="radio" name="typ" value="reservierung">
If anfrage is checked, the script is not submitting this value - there is always reservierung submitting.
Thanks for help!
Martin
I could solve this by:
var formData = new FormData();
if (!form || form.nodeName !== "FORM") {
return;
}
var i, j, q = [];
for (i = form.elements.length - 1; i >= 0; i = i - 1) {
if (form.elements[i].name === "") {
continue;
}
switch (form.elements[i].nodeName) {
case 'INPUT':
switch (form.elements[i].type) {
case 'text':
case 'hidden':
case 'password':
case 'button':
case 'reset':
case 'submit':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'checkbox':
case 'radio':
if (form.elements[i].checked) {
formData.append(form.elements[i].name, form.elements[i].value);
}
break;
case 'file':
break;
}
break;
case 'TEXTAREA':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'SELECT':
switch (form.elements[i].type) {
case 'select-one':
formData.append(form.elements[i].name, form.elements[i].value);
break;
case 'select-multiple':
for (j = form.elements[i].options.length - 1; j >= 0; j = j - 1) {
if (form.elements[i].options[j].selected) {
formData.append(form.elements[i].name, form.elements[i].value);
}
}
break;
}
break;
case 'BUTTON':
switch (form.elements[i].type) {
case 'reset':
case 'submit':
case 'button':
formData.append(form.elements[i].name, form.elements[i].value);
break;
}
break;
}
}

How to combine key codes to work together in javascript

E.g. I have the following script
<script>
document.onkeydown = function(evt){
evt = evt || window.event;
switch (evt.keyCode){
case 67:
createNewFile();
break;
case 82:
goToRecords();
break;
case 84:
goToToday();
break;
case 36:
goToMsHome();
break;
case 27:
escToCloseOptions();
break;
case 83:
summary();
break;
case 73:
insertRecord();
break;
}
};
</script>
When I press
shift + keycode
to call a function specified, I am just new to JavaScript and I code JavaScript based on other languages I know
Thanks and Regards
Check .shiftKey which returns a boolean indicating if the key is pressed. Placing this conditional around your switch will prevent any events from occurring unless the key is pressed in combination with shift.
document.onkeydown = function(evt){
evt = evt || window.event;
if(evt.shiftKey){
switch (evt.keyCode){
case 67:
createNewFile();
break;
case 82:
goToRecords();
break;
case 84:
goToToday();
break;
case 36:
goToMsHome();
break;
case 27:
escToCloseOptions();
break;
case 83:
summary();
break;
case 73:
insertRecord();
break;
}
}
};
You can detect the shift key separately. e.g. if you want to call createNewFile with the shift key down:
if(evt.shiftKey && evt.keyCode == 67) {
createNewFile();
}
In your code, you could do something like:
evt = evt || window.event;
switch (evt.keyCode){
case 67:
if (evt.shiftkey) {
// Do something special for shift mode.
} else {
createNewFile();
}
break;
...

TypeError: name is null when trying to replace problematic characters

In trying to remove problematic characters from imported text via JavaScript, I receive the TypeError: Name is "null" on this line:
for ( var i = 0; i < name.length; i++) {
Here is the relevant section of code -
fixNameToMiniCharset : function (name) {
if(typeof name == 'undefined') {
com.synckolab.global.consoleService.logStringMessage("undefined string " + new Error("s").stack);
}
var ret = "";
// avoid double placeholders
var placeHolder = false;
for ( var i = 0; i < name.length; i++) {
switch (name.charAt(i)) {
// character replaces... better this way
case '\u00e4':
case '\u00e1':
case '\u00e0':
case '\u00e2':
case '\u00e3':
case '\u00e5':
case '\u00e6':
placeHolder = false;
ret += 'a';
break;
case '\u00c4':
case '\u00c1':
case '\u00c0':
case '\u00c2':
case '\u00c3':
case '\u00c5':
case '\u00c6':
placeHolder = false;
ret += 'A';
break;
case '\u00f6':
case '\u00f3':
case '\u00f2':
case '\u00f4':
case '\u00f5':
case '\u00f8':
placeHolder = false;
ret += 'o';
break;
case '\u00d6':
case '\u00d3':
case '\u00d2':
case '\u00d4':
case '\u00d5':
case '\u00D6':
placeHolder = false;
ret += 'O';
break;
case '\u00fc':
case '\u00fa':
case '\u00f9':
case '\u00fb':
case '\u0169':
case '\u0171':
placeHolder = false;
ret += 'u';
break;
case '\u00dc':
case '\u00da':
case '\u00d9':
case '\u00db':
case '\u0168':
placeHolder = false;
ret += 'U';
break;
case '\u00c8':
case '\u00c9':
case '\u00ca':
case '\u00cb':
placeHolder = false;
ret += 'E';
break;
case '\u00e8':
case '\u00e9':
case '\u00ea':
case '\u00eb':
placeHolder = false;
ret += 'e';
break;
case '\u00ec':
case '\u00ed':
case '\u00ee':
case '\u00ef':
placeHolder = false;
ret += 'i';
break;
case '\u00dd':
case '\u00a5':
placeHolder = false;
ret += 'Y';
break;
case '\u00c7':
placeHolder = false;
ret += 'C';
break;
case '\u00e7':
placeHolder = false;
ret += 'c';
break;
case '\u00fd':
case '\u00ff':
placeHolder = false;
ret += 'y';
break;
case '\u00df':
placeHolder = false;
ret += 's';
break;
case '\u00f1':
placeHolder = false;
ret += 'n';
break;
case '\u00b2':
placeHolder = false;
ret += '2';
break;
case '\u00b3':
placeHolder = false;
ret += '3';
break;
// chars which are no problem just stay
case '_':
// make sure to print it only once
if(!placeHolder) {
ret += name.charAt(i);
placeHolder = true;
}
break;
default:
var c = name.charAt(i);
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
placeHolder = false;
ret += c;
} else {
if (!placeHolder) {
ret += '_';
placeHolder = true;
}
}
}
}
return ret;
The problem is quite simple, at first glance: you're checking the argument's type: if it's undefined your function won't run. But in JS null isn't undefined and undefined isn't null.
console.log(typeof null);//object!
console.log(null == undefined);//true
console.log(null === undefined);//false!
So either replace your if (if(typeof name == 'undefined')) by a check on all falsy values:
if(!name)
{
//invalid argument: false, null, 0, '' and undefined
}
//valid args: true, '0', any number, other than 0, strings objects...
Or, perhaps preferable in your case: check if you've got a length property to play with
if (!name || !name.length)
{
//invalid
}
OR check to see if the input is a string:
if (typeof name === 'string')
Change these lines:
if(typeof name == 'undefined') {
com.synckolab.global.consoleService.logStringMessage("undefined string " + new Error("s").stack);
}
to the following:
if(!name) {
com.synckolab.global.consoleService.logStringMessage("undefined string " + new Error("s").stack);
return;
}
This will check for empty undefined and other falsy values which will stop your function from working.

JavaScript Keyboard Event Not Firing

For some very odd reason when you press keys in the order of forward, spacebar, and left. Left does not fire and returns spacebar instead. Any other combination of three keys works perfectly fine, but not that one. Any clues as to why?
var Ctrl = {
init: function() {
window.addEventListener('keydown', this.keyDown, true);
window.addEventListener('keyup', this.keyUp, true);
},
keyDown: function(event) {
console.log(event.keyCode);
switch(event.keyCode) {
case 37: // Left
Ctrl.left = true;
break;
case 39: // Right
Ctrl.right = true;
break;
case 38: // up
Ctrl.up = true;
break;
case 40: // down
Ctrl.down = true;
break;
case 32:
Ctrl.space = true;
break;
default:
break;
}
},
keyUp: function(event) {
switch(event.keyCode) {
case 37: // Left
Ctrl.left = false;
break;
case 39: // Right
Ctrl.right = false;
break;
case 38:
Ctrl.up = false;
break;
case 40:
Ctrl.down = false;
break;
case 32:
Ctrl.space = false;
break;
default:
break;
}
}
};
Maybe one of your keys is activating an unwanted default behavior. You can try to add event.preventDefault(); to your event bindings.
check the jsFiddle
It depends on model of your keyboard. Some keyboards doesn't work with some key combinations. It's normal.

Categories

Resources