DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How to easily check the pressed keyboard button with a converted event using MVVM (Windows Universal)

Marco Siccardi user avatar by
Marco Siccardi
·
Feb. 17, 15 · Interview
Like (0)
Save
Tweet
Share
8.40K Views

Join the DZone community and get the full member experience.

Join For Free

in case you missed it, i lately am deeply diving into mvvm. earlier today, i wanted to implement the well loved feature that a search is performed by pressing the enter button. of course, this would be very easy to achieve in code behind using the keyupevent (or the keydownevent, if you prefer).

however, in mvvm, especially in a universal app, this is a bit trickier. we need to route the event manually to our matching command. there are surely more ways to achieve it, but i decided to use the behaviors sdk to achieve my goal. the first step is of course downloading the matching extension (if you haven’t done so before). to do so, click on tools/extensions and updates in visual studio and install the behaviors sdk from the list: image

the next step we need to do is to add a new converter (i added it to the common folder, you may change this to your preferred place). as we are hooking up the keyupeventargs, i called it keyupeventargsconverter. after you created the class, implement the ivalueconverter interface. you should now have a convert and a convertback method. we are just adding two lines of code to the convert method:

 var args = (keyroutedeventargs)value;
return args;

that’s it for the converter. save the class and build the project. for the next step, we need to go to our view where the converter should do its work. before we can use it, we need to give our converter a key to be identified by the binding engine. you can do this app wide in app.xaml, or in your page:

<common:keyupeventargsconverter x:key="keyupeventargsconverter"/>

also, we need to add two more references to our view (besides the common folder that holds our converter, that is):

 xmlns:i="using:microsoft.xaml.interactivity" 
xmlns:core="using:microsoft.xaml.interactions.core"

the next step is to implement the behavior to our input control (a textbox in my case):

<textbox header="enter search terms" placeholdertext="search terms" text="{binding knowledgebasesearchterms, mode=twoway, updatesourcetrigger=propertychanged}" >
<i:interaction.behaviors>
<core:eventtriggerbehavior eventname="keyup">
<core:invokecommandaction
command="{binding searchtermkeyeventargscommand}"
inputconverter="{staticresource keyupeventargsconverter}">
</core:invokecommandaction>
</core:eventtriggerbehavior>
</i:interaction.behaviors>
</textbox>

with the eventtriggerbehavior, we are able to hook into the desired event of a control. we then only need to bind to a command in our viewmodel and tell the behaviors sdk that it should route the “keyup” event using our converter.

let’s have a final look at the command that handles the event:

 public relaycommand<keyroutedeventargs> searchtermkeyeventargscommand
{
get
{
return _searchtermkeyeventargscommand
?? (_searchtermkeyeventargscommand = new relaycommand<keyroutedeventargs>(
p =>
{
if (p.key == windows.system.virtualkey.enter)
{
//your code here
}
}));
}
}

as you can see, we are using a command that is able to take a generic (in my case it comes from the mvvm light toolkit, but there are several other version floating around). because of this, we are finally getting the keyroutedeventargs into our viewmodel and are able to use its data and properties.

the virtualkey enumeration holds a reference to a lot of (if not all) keys and works for both hardware and virtual keyboards. this makes this code safe to use in an universal app.

as i am quite new to mvvm, i am not entirely sure if this way is the “best” way, but it works as expected with little efforts. i hope this will be useful for some of you.

useful links that helped me on my way to find this solution:

http://blog.galasoft.ch/posts/2014/01/using-the-eventargsconverter-in-mvvm-light-and-why-is-there-no-eventtocommand-in-the-windows-8-1-version/

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868246.aspx

happy coding, everyone!

Event

Published at DZone with permission of Marco Siccardi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT: The Unexpected API Test Automation Help
  • Load Balancing Pattern
  • Stream Processing vs. Batch Processing: What to Know
  • An Introduction to Data Mesh

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: