When should I use keydown and keyup? - keyevent

Which event should be used for key press handling key-down/key-up? It is sure that in both case the program will run successfully. But which one will be more user-friendly?

It depends on you. There is no such best practice. Both are used as per the need of your program and as per the convenience of the user.
keyup
Fires when the user releases a key, after the default action of that key has been performed.
keydown
Fires when the user depresses a key. It repeats while the user keeps the key depressed.
Check out this page describing the differences.

Related

What is the correct way to cancel a RegNotifyChangeKeyValue?

Asynchronous usage of RegNotifyChangeKeyValue is simple enough: pass it an event object and wait for the event to be signalled.
What is not so clear, however, is the correct way to cancel a notification request. For example, if a timeout has been set on the wait like so:
RegNotifyChangeKeyValue(hKey, false, REG_NOTIFY_CHANGE_LAST_SET, regEvent.SafeWaitHandle, true);
regEvent.WaitOne(TimeSpan.FromMinutes(30))
If the timeout occurs without the notify firing, this notification is left presumably pending and waiting to fire. This is worse if the notify and wait is within a loop, potentially registering many notifications (one for each expired timeout).
Do I simply close both the event handle (regEvent) and the registry key handle (hKey)? Is there anything else I need to call, e.g. directory change notifications have FindCloseChangeNotification, is there an equivalent here?
I'm currently using P/Invoke with C#, but I do not believe that should make a difference - any answer should be focused on the requirements and usage of the Windows API. This would be much the same question if I had used RegNotifyChangeKeyValue/CreateEvent/WaitForSingleObject from VC++.
Closing the open Registry key is enough. When the key is closed, the event is signaled. This is documented behavior. So, simply be sure to close the Registry key before freeing the event.

How to track anonymous events in Mixpanel

mixpanel's documentation says that
tracker.track(user_id, 'Sent Message')
which requires a user_id to be the source of the event.
What do I do if the user isn't logged it or the event just happens without the user interaction? for example, a redirect caused by some dynamic data
is there a way to track event (with the ruby api) that isn't matched to a user?
It is possible to track an event without a distinct_id (aka user_id) at the API level, but it's not a good idea because this event will not be usable in any report except "Segmentation" with the "Total" parameter (i.e. it will be useless in Segmentation+Unique, Retention, and Funnel reports). At that point, it's likely better to just not send anything, since it can never be attributed to a user.
If you're really just looking for counts of an event, you can simply generate a UUID. Don't default to -1, null, or another catchall as this can have unintended consequences.

How can I store data when was deactivated event fired

When user click the Bing Search or Start button, this will cause Deactivated event.
So, when user press Bing Search or Start Button, How do I store the data. What type of Data can be stored and What to use to store?
You can save:
Page state (textbox values, scroll positions) by overriding OnNavigatedFrom and writing values to the Page's State property. You can reload the data in OnNavigatedTo
Application state (stuff that applies to all pages that you'd keep if the user hit back to return to the application, but not if they re-launched the application from Start) by handling the Activated / Deactivated events of PhoneApplicationPage and storing data in its State property. If targetting Mango, you can (and should) skip loading your application state if ActivatedEventArgs.IsApplicationInstancePreserved is true.
Permanent state (data caches, encrypted user sessions keys) to the file system using IsolatedStorageFile. It's better to do this when you receive that data, rather than waiting for the Deactivated event as taking too long to write data can result in your application being terminated (and corrupting your isolated storage files)
Page/application state dictionaries can store simple types as well as dictionaries and any serialiazable class (which has an empty constructor requirement).
You need to "tombstone" your data. First of all, read about the Execution Model for Windows Phone
After that, read one of the many guides on the subject.
You can use IsolatedStorage or the Application State environment to store information. Sorry to say, but start googling.. you would probably found this if you did.

Cocoa/Carbon: catch global hotkey when key is kept pressed (held down)

Using InstallApplicationEventHandler and RegisterEventHotKey from Carbon framework, I'm able to catch whatever key I want.
On the contrary I can't find a way to handle correctly the event when a key is held down (kept pressed). I mean, when I held down a key, RegisterEventHotKey responds like if I have pressed and then released the key; I want it, instead, to send the "event" continuously until the key is released.
What I really want to achieve, to be precise, when a key is kept pressed is this:
as soon as the key is pressed and held down I want that InstallApplicationEventHandler and RegisterEventHotKey create an event but not only once (as I have now) but every x milliseconds or so until the key is released.
The particular keys that I'm considering are not modifier keys.
Can you help me? I'm really becoming crazy on this!
You need to use an event tap; RegisterEventHotKey isn't flexible enough to do what you want. Check out Event Taps Testbench and the documentation.
Maybe start firing a timer every 0.25 seconds or something that checks to see if the appropriate keys are still down. If they are, re-invoke the action. Otherwise, invalidate the timer.

Auto-save with Cocoa and Core Data

I am working on a non-document-based Core Data application.
I would like changes to be saved as they happen. This is what the user expects in this type of application. It is also what Apple has implemented in iPhoto or iTunes.
A brute force approach would be to set up a timer to save frequently. The method triggered by the saving would then swallow all validation errors so as not to bother the user. Only upon quitting, the user will be bugged to arrange the data so it can save. IMHO, that approach stinks.
So I am thinking, there must be a way to somehow hook saving to something like the NSEditor protocol. Every time the user (or a controller) finishes editing data, the application delegate should somehow be notified an trigger a save operation. Thing is I don't quite know where to look.
I would think that for more complicated operations, which may require some cross-validations to go through, I would present the user with bit of interface tied to a dedicated NSManagedObjectContext.
At the end of each event in an AppKit app, CoreData will run a -processPendingTransactions for you.
One side-effect of this is that if you've registered with your NSManagedObjectContext to receive change notifications, you'll get called at the end of each event.
So, for instance, in your notification handler, you could call just tell the context to save.
However, you might be paranoid about doing a save on a context while in a callback from that same context, so you'd probably feel better if you did a performSelector:#selector(save:) afterDelay: to push the save until after the -processPendingTransactions is done.
You could even do a cancel previous on the -save: selector and have the delay be like 5 seconds, so if the user or app is in the middle of a BUNCH of changes they'll all coalesce into a single save.
And, in fact, that's exactly how Delicious Library 1.0-1.09 worked.
-Wil

Resources