473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

WPF Keyboard input UI thread issue?

464 Expert 256MB
I have a simple application which includes a default window (named Window1) with no additional xaml.

Window1 consists of a timerclass object which contains a timer running at 60hrz.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public Window1()
  3.         {//in Window1.xaml.cs
  4.             MainTimerClass = new TimerClass(); //my timer class object
  5.         }
The Timer class consists of an object which handles keyboard operations
Keyboard class is defined as below.

Expand|Select|Wrap|Line Numbers
  1.  class KeyboardHandle
  2.     {
  3.         public KeyboardHandle()
  4.         {
  5.         }
  6.  
  7.         public void ProcessKeyboardEvent(ref Hashtable MappedKeyTable)
  8.         {
  9.             foreach (DictionaryEntry de in MappedKeyTable)
  10.             {
  11.                 Key TempKey = (Key)de.Key;
  12.                 if(Keyboard.IsKeyDown(TempKey))
  13.                 {
  14.                     int pq = (int)de.Value;
  15.                 }
  16.             }
  17.         }
  18.  
The Timer calls the ProcessKeyboardEvent 60 times a second. When i hit the line of code Keyboard.IsKeyDown(TempKey)) i get the runtime exception "The calling thread must be STA, because many UI components require this"

Basically I want my code to check a given set of keyboard keys to see if they are pressed or not. I saw that the GUI element of WPF runs on a different thread and I have to use the Dispatcher.Invoke methods, but 1.) Why is the keyboard tied to a UI thread? two How can I use the dispatcher.Invoke to get the keyboard state? Any ideas on how to do this? I could just use windows hooks or Wnd_Proc, but I would prefer not to... Any ideas/suggestions? Let me know if you need anything else posted.
Jan 12 '10 #1
0 2785

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

Similar topics

23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
4
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
4
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard,...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
1
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the...
1
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the...
0
by: clayga | last post by:
Hi, Trying to help my sons do some VB programming. they've got hold of some books with simple program listings but they're all 1980's BASIC (ZX81 et al). The biggest problem I've found so far...
3
by: NaN | last post by:
I've been trying to use _kbhit() but it didn't do what I thought it would from books, "Detects whether a keypress is available for reading." Herbert Schildt says, "If the user has pressed a key,...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.