Repository: kivy/kivy

Issue:
Examples: Keyboard & KeyboardListener
### Versions

* Python: 2.7, 3.5
* OS: Win8.1, 64bit
* Kivy: master, wheels

### Description

Example `examples/keyboard/main.py` dead for py3(.5.3).

> UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 3960

Similar for `examples/widgets/keyboardlistener.py` after pressed modifier (Ctrl, Shift)

> UnicodeEncodeError: 'charmap' codec can't encode character '\u0131' in position 12 ...


Discussion:
Both work on python 3.6.0 on OS X,  could you upgrade your python version and test again ?
Yep, dead with the same error on 3.6 too. I run in `cmd.exe` and although they changed the encoding in [pep528](https://www.python.org/dev/peps/pep-0528/) and 529, this didn't fix it, so the problem is probably somewhere in the conversion unicode↔bytes

```
 Traceback (most recent call last):
   File "share\kivy-examples\keyboard\main.py", line 222, in <module>
     KeyboardDemo().run()
   File "_\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "share\kivy-examples\keyboard\main.py", line 216, in build
     self.sm.add_widget(KeyboardScreen(name="keyboard"))
   File "share\kivy-examples\keyboard\main.py", line 163, in __init__
     self._add_keyboards()
   File "share\kivy-examples\keyboard\main.py", line 169, in _add_keyboards
     layouts = list(VKeyboard().available_layouts.keys())
   File "_\lib\site-packages\kivy\uix\vkeyboard.py", line 367, in __init__
     self._load_layouts()
   File "_\lib\site-packages\kivy\uix\vkeyboard.py", line 433, in _load_layouts
     basename(splitext(fn)[0]))
   File "_\lib\site-packages\kivy\uix\vkeyboard.py", line 440, in _load_layout_fn
     json_content = fd.read()
   File "_\lib\encodings\cp1250.py", line 23, in decode
     return codecs.charmap_decode(input,self.errors,decoding_table)[0]
 UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 3960: character maps to <undefined>
```
Have you tried running with `PYTHONLEGACYWINDOWSSTDIO=1` set in the env?
Yes, doesn't do anything useful with the error (log is the same).
https://github.com/kivy/kivy/commits/e7299f4e7f6eb3ddf773c26a303af1dd52b90e27/kivy/data/keyboards everything since https://github.com/kivy/kivy/commit/bcca12b3032958992d5fea6a3c2fa9d2b6f6b90f up crippled it.

I'm not sure if there is some problem with the jsons themselves, because I'm not sure if you are supposed to just prefix everything with `u` like for py2 unicode or not. I rather believe the json isn't loaded properly in py3 with the right encoding, thus throws errors.

Resolving patch:
diff --git a/examples/keyboard/main.py b/examples/keyboard/main.py
--- a/examples/keyboard/main.py
+++ b/examples/keyboard/main.py
@@ -204,7 +204,11 @@ def key_down(self, keyboard, keycode, text, modifiers):
     # def key_up(self, keyboard, keycode):
     def key_up(self, keyboard, keycode, *args):
         """ The callback function that catches keyboard events. """
-        self.displayLabel.text += u" (up {0[1]})".format(keycode)
+        # system keyboard keycode: (122, 'z')
+        # dock keyboard keycode: 'z'
+        if isinstance(keycode, tuple):
+            keycode = keycode[1]
+        self.displayLabel.text += u" (up {0})".format(keycode)
 
 
 class KeyboardDemo(App):
diff --git a/kivy/uix/vkeyboard.py b/kivy/uix/vkeyboard.py
--- a/kivy/uix/vkeyboard.py
+++ b/kivy/uix/vkeyboard.py
@@ -124,6 +124,7 @@
 from kivy.resources import resource_find
 from kivy.clock import Clock
 
+from io import open
 from os.path import join, splitext, basename
 from os import listdir
 from json import loads
@@ -446,7 +447,7 @@ def _load_layout_fn(self, fn, name):
         available_layouts = self.available_layouts
         if fn[-5:] != '.json':
             return
-        with open(fn, 'r') as fd:
+        with open(fn, 'r', encoding='utf-8') as fd:
             json_content = fd.read()
             layout = loads(json_content)
         available_layouts[name] = layout


Test patch: