473,466 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

disabling keys: limit user to alphabet and numbers

59 New Member
when user press can key than the code below gets the key user just pressed. the problem is that i want to only limit user to press alphabet and numbers. so i want to disable weird char ex left, right, up, @,$,!,%..... as you can see this will take up alot of if statments. is there better way to do this?


Expand|Select|Wrap|Line Numbers
  1. if(keys == KeyEvent.VK_SHIFT || keys == KeyEvent.VK_CAPS_LOCK || keys ==KeyEvent.VK_LEFT ||
  2.    keys ==KeyEvent.VK_RIGHT || keys ==KeyEvent.VK_DOWN || keys ==KeyEvent.VK_UP   .....)  //more button to disable
  3. {
  4. }
  5. else{  //store the char in array            
  6.     username[z] = e.getKeyChar();
  7.     z++;
  8. }
  9.  
May 3 '13 #1
4 2022
Rabbit
12,516 Recognized Expert Moderator MVP
Rather than blacklisting the keys, whitelist the ranges that are allowed.
May 3 '13 #2
game2d
59 New Member
whats whitlist the range? is there tutorial online how to do this?
May 3 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
I don't know of a tutorial, but what Rabbit means is the following:
As you can see in the KeyEvent API docs, all of these events are basically integers and can be compared as such. So you can check which keys should be allowed (hence a whitelist) by something like this:
Expand|Select|Wrap|Line Numbers
  1. if( (keys >= KeyEvent.VK_0 && keys <= KeyEvent.VK_9)
  2.  || (keys >= KeyEvent.VK_A && keys <= KeyEvent.VK_Z)) {
  3.   // do stuff
  4. } else {
  5.   // Tell the user he pressed a deactivated key. Or don't. Your choice.
  6. }
May 4 '13 #4
game2d
59 New Member
ah i see. i didnt knew we could do this. thanks alot guys
May 4 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Niels Borg | last post by:
Hi, I am building an app, where I would like to disable the standard windows function keys. I.E. F4 which expands a a selected drop-down list. Can anyone help me on that? Many thanks Niels...
5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
5
by: glenn.owens | last post by:
In the process of doing some routine monitoring/clean-up we've discovered that several (many?) users are apparently set to access our SQL Server 2000 database instances via the Named Pipes...
3
by: Armando | last post by:
Apologies if this has been covered recently - I lurked and saw nothing. I have an application that does ticketing at a three-screen movie theatre, and I'm using the function keys F1 thru F12...
3
by: PB | last post by:
What is the rationalle for disabling JavaScript. AFAIK, the primary reason is for "security purposes" - but what specific kind of threats does the protect against? AND - is the disabling of...
12
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation...
1
by: JJ | last post by:
I have a createuserwizard control on my web form. When the user registration fields are filled out correctly, and the user then presses the return key instead of clicking the 'create user' button,...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
2
by: Duckof85 | last post by:
How do I restore a programs default funtion keys? After installing the software for the new keyboard, the only functions I can preform are the ones from the software. I would like to be able to the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.